jump-start 0.1.1-alpha-7

The CLI for jump-start: A shortcut to your favorite code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use crate::JumpStartInstance;
use crate::starter::StarterConfig;
use crate::{Config, LocalStarter, RemoteStarter, config::get_default_instance};
use anyhow::{Context, Result};
use directories::ProjectDirs;
use flate2::read::GzDecoder;
use log::{debug, info, warn};
use reqwest::blocking::Client;
use std::fs::{self, File};
use std::io;
use std::path::{Path, PathBuf};
use tar::Archive;

pub fn r#use(config: Config, starter_identifier: &str, dest: Option<&str>) -> Result<()> {
    let instance = get_default_instance(&config);

    if starter_identifier.starts_with("@") {
        let starter = RemoteStarter::from_path(starter_identifier).unwrap();
        debug!("Remote starter {:?}", starter);
        let mode = "tar";

        let ultimate_dest = clone_remote_starter(starter, dest, mode)
            .with_context(|| format!("Cloning remote starter"))?;
        info!("{} copied to {:?}", starter_identifier, ultimate_dest)
    } else {
        let starter = LocalStarter::from_path(starter_identifier).unwrap();
        debug!("Local starter {:?}", starter);

        let ultimate_dest = clone_local_starter(instance, starter, dest)
            .with_context(|| format!("Cloning local starter"))?;
        info!("{} copied to {:?}", starter_identifier, ultimate_dest)
    }

    Ok(())
}

fn download_tar(url: &String, dest: &Path) -> Result<PathBuf> {
    fs::create_dir_all(dest)?;

    let file_path = dest.join("HEAD.tar.gz");
    // TODO is this the behavior I want?
    if file_path.exists() {
        debug!("{} already exists locally", file_path.display());
        return Ok(file_path);
    }

    info!("Downloading {} to {}", url, file_path.display());
    debug!("Starting download for tarball from {}", url);

    let client = Client::new();
    let mut response = client.get(url).send()?;

    if !response.status().is_success() {
        warn!("HTTP request failed with status: {}", response.status());
        anyhow::bail!("Failed to download tar: {}", url);
    }

    let mut file = File::create(&file_path)?;
    io::copy(&mut response, &mut file)?;

    Ok(file_path)
}

/// Extracts a subdirectory from a tar.gz archive file to a destination path.
fn extract_tar_subdir(tar_path: &Path, subdir: &str, dest: &Path) -> Result<()> {
    fs::create_dir_all(dest)?;

    let tar_file = File::open(tar_path)?;
    let tar = GzDecoder::new(tar_file);
    let mut archive = Archive::new(tar);
    let mut subdir_found = false;

    for entry in archive.entries()? {
        let mut entry = entry?;
        let path = entry.path()?;
        let path_str = path.to_string_lossy();

        let parts: Vec<&str> = path_str.split('/').collect();

        if parts.len() <= 1 {
            // Skip the root directory itself
            continue;
        }

        // Remove the repository root directory (first component)
        // This handles names like "jump-start-b3c8d936025b11b9a57cfac99e0decb9f908042e/"
        let rel_path_str = parts[1..].join("/");
        let rel_path = PathBuf::from(&rel_path_str);
        // println!("rel_path: {:?}", rel_path);

        if rel_path.as_os_str().is_empty() {
            continue;
        }

        // Check if the file path is in the subdirectory using Path components
        // This is more robust than string manipulation
        let subdir_path = Path::new(subdir);

        // Get the components from both paths for comparison
        let rel_path_components: Vec<_> = rel_path.components().collect();
        let subdir_components: Vec<_> = subdir_path.components().collect();

        // Check if rel_path has at least the same components as subdir
        if rel_path_components.len() < subdir_components.len() {
            continue;
        }

        // Check if all subdir components match the beginning of rel_path components
        let is_match = subdir_components
            .iter()
            .zip(rel_path_components.iter())
            .all(|(a, b)| a == b);

        if !is_match {
            continue;
        }

        // Use the components we already collected
        let final_path = rel_path_components
            .into_iter()
            .skip(subdir_components.len())
            .collect::<PathBuf>();

        // Skip if this was just the directory itself
        if final_path.as_os_str().is_empty() {
            continue;
        }

        let dest_path = dest.join(final_path);

        if let Some(parent) = dest_path.parent() {
            fs::create_dir_all(parent)?;
        }

        entry.unpack(&dest_path)?;

        subdir_found = true;
    }

    if subdir_found {
        Ok(())
    } else {
        anyhow::bail!("Subdirectory '{}' not found in archive", subdir)
    }
}

/// Recursively copy the directory `src` to `dest`
fn copy_dir_contents(src: &Path, dest: &Path) -> Result<()> {
    fs::create_dir_all(dest)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let src_path = entry.path();
        let dst_path = dest.join(entry.file_name());

        if file_type.is_dir() {
            fs::create_dir_all(&dst_path)?;
            copy_dir_contents(&src_path, &dst_path)?;
        } else {
            fs::copy(&src_path, &dst_path)?;
        }
    }

    Ok(())
}

/// Download a starter from GitHub, storing it in `dest`.
///
/// `mode` should be "tar" or "git" but "git" is not yet implemented.
fn clone_remote_starter(starter: RemoteStarter, dest: Option<&str>, mode: &str) -> Result<PathBuf> {
    if mode == "tar" {
        let project_dirs = ProjectDirs::from("", "", "jump-start")
            .unwrap_or_else(|| panic!("Could not find OS project directory"));

        let cache_dir = project_dirs
            .cache_dir()
            .join("github")
            .join(&starter.github_username)
            .join(&starter.github_repo);

        let tar_url = format!(
            "https://www.github.com/{}/{}/archive/HEAD.tar.gz",
            starter.github_username, starter.github_repo
        );
        let tar_path = download_tar(&tar_url, &cache_dir)
            .with_context(|| format!("Downloading tar {}", tar_url))?;
        let subdir = format!("{}/{}", starter.group, starter.name);
        let cache_dest = cache_dir.join(&subdir);

        extract_tar_subdir(&tar_path, &subdir, &cache_dest)
            .with_context(|| format!("Extracting tar {} into {:?}", tar_url, cache_dest))?;
        debug!(
            "Extracted {:?} with subdir {:?} to {:?}",
            tar_path, subdir, cache_dest
        );

        let final_dest = get_final_dest(&cache_dest, dest)?;

        copy_dir_contents(&cache_dest, &final_dest)
            .with_context(|| format!("Copying dir contents"))?;
        Ok(final_dest)
    } else {
        anyhow::bail!("Not implemented")
    }
}

/// Get the final destination for the starter according to these rules:
///
/// 1. If `dest` is specified, use that
/// 2. If the starter's jump-start.yaml file has "defaultDir" specified, use that
/// 3. Otherwise use "."
fn get_final_dest(starter_path_full: &PathBuf, dest: Option<&str>) -> Result<PathBuf> {
    let final_dest = match dest {
        Some(dest) => PathBuf::from(&dest),
        None => {
            let starter_config_path = starter_path_full.join("jump-start.yaml");
            let file_content = fs::read_to_string(&starter_config_path)?;
            let starter_config = file_content
                .parse::<StarterConfig>()
                .unwrap_or_else(|e| panic!("Could not parse yaml: {:?} {}", starter_path_full, e));

            // Default to "." if default_dir is None
            match starter_config.default_dir {
                Some(dir) if !dir.as_os_str().is_empty() => dir,
                _ => PathBuf::from("."),
            }
        }
    };

    Ok(final_dest)
}

/// Copy a starter into `dest`.
fn clone_local_starter(
    instance: &JumpStartInstance,
    starter: LocalStarter,
    dest: Option<&str>,
) -> Result<PathBuf> {
    let starter_path_full = instance.path.join(starter.path);
    let final_dest = get_final_dest(&starter_path_full, dest)?;

    copy_dir_contents(&starter_path_full, &final_dest)?;
    Ok(final_dest)
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use tar::Builder;
    use tempfile::{TempDir, tempdir};

    fn create_test_archive(
        root_dir: &str,
        _target_subdir: &str,
        files: Vec<(String, &str)>,
    ) -> Result<(TempDir, PathBuf)> {
        let temp_dir = tempdir()?;
        let archive_path = temp_dir.path().join("test_archive.tar.gz");

        let tar_gz = File::create(&archive_path)?;
        let enc = GzEncoder::new(tar_gz, Compression::default());
        let mut builder = Builder::new(enc);

        for (file_path, contents) in files {
            let full_path = format!("{}/{}", root_dir, file_path);

            let mut header = tar::Header::new_gnu();
            header.set_size(contents.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder.append_data(&mut header, full_path, contents.as_bytes())?;
        }

        builder.finish()?;
        Ok((temp_dir, archive_path))
    }

    #[test]
    fn test_extract_tar_subdir() -> Result<()> {
        let root_dir = "test-repo-root";
        let subdir_path = "test-group/test-starter";

        let files = vec![
            (
                format!("{}/file1.txt", subdir_path),
                "This is file 1 content",
            ),
            (
                format!("{}/file2.txt", subdir_path),
                "This is file 2 content",
            ),
            (
                format!("{}/nested/file3.txt", subdir_path),
                "This is a nested file",
            ),
            (
                "other-dir/file4.txt".to_string(),
                "This file should not be extracted",
            ),
            (
                "test-group/other-starter/file5.txt".to_string(),
                "This file should not be extracted",
            ),
        ];

        let (_archive_temp, archive_path) = create_test_archive(root_dir, subdir_path, files)?;
        let extract_temp = tempdir()?;
        let extract_path = extract_temp.path();

        extract_tar_subdir(&archive_path, subdir_path, extract_path)?;

        assert!(
            extract_path.join("file1.txt").exists(),
            "file1.txt should be extracted"
        );
        assert!(
            extract_path.join("file2.txt").exists(),
            "file2.txt should be extracted"
        );
        assert!(
            extract_path.join("nested/file3.txt").exists(),
            "nested/file3.txt should be extracted"
        );

        assert_eq!(
            fs::read_to_string(extract_path.join("file1.txt"))?,
            "This is file 1 content"
        );
        assert_eq!(
            fs::read_to_string(extract_path.join("file2.txt"))?,
            "This is file 2 content"
        );
        assert_eq!(
            fs::read_to_string(extract_path.join("nested/file3.txt"))?,
            "This is a nested file"
        );

        assert!(
            !extract_path.join("other-dir/file4.txt").exists(),
            "file4.txt should not be extracted"
        );
        assert!(
            !extract_path
                .join("test-group/other-starter/file5.txt")
                .exists(),
            "file5.txt should not be extracted"
        );

        Ok(())
    }

    #[test]
    fn test_extract_tar_subdir_nonexistent_subdir() -> Result<()> {
        let root_dir = "test-repo-root";
        let real_subdir = "test-group/test-starter";

        let files = vec![(
            format!("{}/file1.txt", real_subdir),
            "This is file 1 content",
        )];

        let (_archive_temp, archive_path) = create_test_archive(root_dir, real_subdir, files)?;
        let extract_temp = tempdir()?;
        let extract_path = extract_temp.path();

        let nonexistent_subdir = "nonexistent/directory";
        let result = extract_tar_subdir(&archive_path, nonexistent_subdir, extract_path);

        assert!(
            result.is_err(),
            "Function should return an error for nonexistent subdirectory"
        );

        if let Err(e) = result {
            assert!(e.to_string().contains("not found in archive"));
        }

        Ok(())
    }

    #[test]
    fn test_extract_tar_subdir_partial() -> Result<()> {
        let root_dir = "test-repo-root";
        let real_subdir = "test-group/test-starter";

        let files = vec![(
            format!("{}/file1.txt", real_subdir),
            "This is file 1 content",
        )];

        let (_archive_temp, archive_path) = create_test_archive(root_dir, real_subdir, files)?;
        let extract_temp = tempdir()?;
        let extract_path = extract_temp.path();

        let partial_subdir = "test-group/test-star";
        let result = extract_tar_subdir(&archive_path, partial_subdir, extract_path);

        assert!(
            result.is_err(),
            "Function should return an error for subdirectory name that is only partially included"
        );

        if let Err(e) = result {
            assert!(e.to_string().contains("not found in archive"));
        }

        Ok(())
    }

    #[test]
    fn test_copy_dir_contents() -> Result<()> {
        let temp_dir = tempdir()?;
        let src_dir = temp_dir.path().join("src");
        let dest_dir = temp_dir.path().join("dest");

        // Create source directory with some files
        fs::create_dir_all(src_dir.join("nested"))?;
        fs::write(src_dir.join("file1.txt"), "test content 1")?;
        fs::write(src_dir.join("nested/file2.txt"), "test content 2")?;

        copy_dir_contents(&src_dir, &dest_dir)?;

        // Verify files were copied correctly
        assert!(dest_dir.join("file1.txt").exists());
        assert!(dest_dir.join("nested/file2.txt").exists());

        // Check file contents
        let file1_content = fs::read_to_string(dest_dir.join("file1.txt"))?;
        assert_eq!(file1_content, "test content 1");
        Ok(())
    }

    #[test]
    fn test_get_final_dest_explicit() -> Result<()> {
        let temp_dir = tempdir()?;
        let starter_path = temp_dir.path().join("starter");
        fs::create_dir_all(&starter_path)?;

        // Create a starter config
        let config_content = "name: test-starter\ndefaultDir: ./default-project";
        fs::write(starter_path.join("jump-start.yaml"), config_content)?;

        let explicit_dest = "explicit-dest";
        let final_dest = get_final_dest(&starter_path, Some(explicit_dest))?;

        assert_eq!(final_dest, PathBuf::from(explicit_dest));

        Ok(())
    }

    #[test]
    fn test_get_final_dest_from_config() -> Result<()> {
        let temp_dir = tempdir()?;
        let starter_path = temp_dir.path().join("starter");
        fs::create_dir_all(&starter_path)?;

        // Create a starter config
        let config_content = "name: test-starter\ndefaultDir: ./default-project";
        fs::write(starter_path.join("jump-start.yaml"), config_content)?;

        let final_dest = get_final_dest(&starter_path, None)?;

        assert_eq!(final_dest, PathBuf::from("./default-project"));

        Ok(())
    }

    #[test]
    fn test_get_final_dest_empty_default() -> Result<()> {
        let temp_dir = tempdir()?;
        let starter_path = temp_dir.path().join("starter");
        fs::create_dir_all(&starter_path)?;

        // Create a starter config with empty defaultDir
        let config_content = "name: test-starter\ndefaultDir: \"\"";
        fs::write(starter_path.join("jump-start.yaml"), config_content)?;

        let final_dest = get_final_dest(&starter_path, None)?;

        assert_eq!(final_dest, PathBuf::from("."));

        Ok(())
    }
}