Skip to content

Commit

Permalink
v2: refactor, redesign commands (#11)
Browse files Browse the repository at this point in the history
* v2: refactor, redesign commands

* test: windows happy

* test: windows happy
  • Loading branch information
jondot authored Nov 19, 2022
1 parent 83b1af2 commit 797182f
Show file tree
Hide file tree
Showing 32 changed files with 422 additions and 404 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 38 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,36 @@

```
$ bp --help
backpack 1.5.0
Create projects from existing repos
backpack 2.0.0
Set up projects and download files from existing repos
USAGE:
bp <SUBCOMMAND>
bp [OPTIONS] [ARGS] [SUBCOMMAND]
ARGS:
<shortlink> A full or short link to a repo (e.g. org/user)
<dest> Target folder
OPTIONS:
-h, --help Print help information
-V, --version Print version information
-c, --config <config> Use a specified configuration file
-f, --fetch Fetch and apply into the current folder
-g, --git Clone with git
-h, --help Print help information
-n, --no-cache Fetch resources without using the cache
-V, --version Print version information
-w, --overwrite Always overwrite target file(s)
SUBCOMMANDS:
add Add a repo as a project
apply apply remote files into a folder [aliases: a]
cache cache handling
config create custom configuration
add Save a repo as a project
cache Cache handling
config Create a personal configuration
help Print this message or the help of the given subcommand(s)
new initialize a new project [aliases: n]
```

To generate a new project, you can use any repo:
To download and generate a new project, you can use any repo:

```
$ bp new your/repo
$ bp your/repo
```

## Download
Expand Down Expand Up @@ -102,7 +109,7 @@ projects:
And now run:
```
$ bp new rust-starter
$ bp rust-starter
```

To personalize a project you can take input and replace content:
Expand Down Expand Up @@ -149,15 +156,15 @@ new:
path: .*
```

You can set different actions and swaps for when people do `bp new` vs `bp apply`.
You can set different actions and swaps for when people do `bp` vs `bp -f` (fetch and apply to current folder).

For file operations such as renaming, moving and so on, you can use vanilla actions (`mv x y`, `rm x`).
</details>


## :raising_hand_woman: Configure user projects

`bp new` will automatically display a list of projects if configure those.
`bp` (with no args) will automatically display a list of projects if configure those.

Projects define repos and custom actions and / or swaps you want to attach to each.

Expand Down Expand Up @@ -304,7 +311,7 @@ vendors:
And now, you can use the `ghe:` prefix for your shortlinks:

```
$ bp new ghe:user/repo
$ bp ghe:user/repo
```

You can check in the `.backpack.yaml` to your project to share it with your team. When `backpack` runs it will **pick it up automatically**.
Expand All @@ -317,27 +324,27 @@ $ bp config --init --global
</details>

<details>
<summary><b>What's the difference between `new` and `apply`?</b></summary>
<summary><b>What's the difference between `bp` and `bp -f`?</b></summary>

```
$ bp new kriasoft/react-starter-kit my-react-project
$ bp kriasoft/react-starter-kit my-react-project
```

* Use `new` to create **a new project** into `my-react-project`
* Create **a new project** into `my-react-project`
* Resolves to [https://github.com/kriasoft/react-starter-kit](https://github.com/kriasoft/react-starter-kit)
* Finds the default branch, downloads it and caches locally. Next time you run, it'll be much faster.




```
$ bp apply kriasoft/react-starter-kit/-/.github
$ bp -f kriasoft/react-starter-kit/-/.github
```

Let's say you really like how `react-starter-kit` configured its Github Action, and you'd like to copy that to your **existing project**. You can do this:

* Use `/-/` to access a subfolder
* Use `apply` to overlay files onto your current working directory
* Use `-f` to overlay files onto your current working directory

</details>

Expand All @@ -348,7 +355,7 @@ Let's say you really like how `react-starter-kit` configured its Github Action,
To maximize producitivity, you can do either of these, or all of these in sequence:

1. Just copy material from a template repo, as a faster `git clone` that has built-in cache and knows how to take **parts of repos**.
2. Embed **placeholder variables** in your template repo and have `backpack` swap these when doing `bp new` or `bp apply`
2. Embed **placeholder variables** in your template repo and have `backpack` swap these when doing `bp` or `bp -f`
3. **Execute actions** for input taking from a user, or for running install actions after a clone

You can build a `.backpack-project.yml` into your template repo for defining actions and variables, or a `project` pointing to that repo in your central `backpack.yml`.
Expand All @@ -363,9 +370,7 @@ You can build a `.backpack-project.yml` into your template repo for defining act


```
$ bp new
or
$ bp apply
$ bp
```

And follow the interactive menu, which will let you:
Expand Down Expand Up @@ -406,7 +411,7 @@ user/repo#wip -> takes the 'wip' branch
Yes, use the folder notation `/-/`:

```
$ bp new user/repo/-/path/to/folder dest-folder
$ bp user/repo/-/path/to/folder dest-folder
```
</details>

Expand All @@ -417,17 +422,17 @@ $ bp new user/repo/-/path/to/folder dest-folder
Branches or tags can be used with the `#branch` specifier.

```
$ bp new kriasoft/react-starter-kit#feature/redux my-starter
$ bp kriasoft/react-starter-kit#feature/redux my-starter
```
</details>

<details><summary><b> Can I use backpack on empty or populated directories?</b></summary>

Yes. Use `apply` to grab content and apply it to an existing empty or populated directories:
Yes. Use `-f` to grab content and apply it to an existing empty or populated directories:

```
$ cd your-directory
$ bp apply user/repo .
$ bp -f user/repo
```

</details>
Expand All @@ -448,7 +453,7 @@ vendors:
Note that in addition to the custom hosted `github.acme.com` server, we also specified a default org `my-org` above, so it saves a bit of typing. Then you can run:

```
$ bp new gh:my-repo my-repo
$ bp gh:my-repo my-repo
```
</details>
Expand All @@ -457,7 +462,7 @@ $ bp new gh:my-repo my-repo
Where it's non ambiguous, yes. For example, when you specify a subfolder:
```
$ bp new user/repo/-/my-folder
$ bp user/repo/-/my-folder
```
Will grab just `my-folder` from `user/repo` and create in a destinaton folder called `my-folder`.
Expand Down Expand Up @@ -485,7 +490,7 @@ For example, this will give you a `.gitignore` file from another project:
```
$ cd my-project
$ bp apply rusty-ferris-club/backpack/-/.gitignore
$ bp -f rusty-ferris-club/backpack/-/.gitignore
$ tree
.gitignore
```
Expand All @@ -494,7 +499,7 @@ This will copy just a single workflow file, but also the entire hierarchy of fol
```
$ cd my-project
$ bp apply rusty-ferris-club/backpack/-/.github/workflows/build.yml
$ bp -f rusty-ferris-club/backpack/-/.github/workflows/build.yml
$ tree
.github/
workflows/
Expand Down
2 changes: 1 addition & 1 deletion backpack/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "backpack"
version = "1.5.0"
version = "2.0.0"
edition = "2021"


Expand Down
10 changes: 4 additions & 6 deletions backpack/src/bin/bp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ fn main() {
.init();

let app = commands::root::command()
.subcommand(commands::new::command())
.subcommand(commands::apply::command())
.subcommand(commands::cache::command())
.subcommand(commands::add::command())
.subcommand(commands::config::command());
Expand All @@ -25,14 +23,14 @@ fn main() {

let res = match matches.subcommand() {
Some(tup) => match tup {
("new", subcommand_matches) => commands::new::run(&matches, subcommand_matches),
("apply", subcommand_matches) => commands::apply::run(&matches, subcommand_matches),
("cache", subcommand_matches) => commands::cache::run(&matches, subcommand_matches),
("add", subcommand_matches) => commands::add::run(&matches, subcommand_matches),
("config", subcommand_matches) => commands::config::run(&matches, subcommand_matches),
_ => unreachable!(),
(maybe_shortlink, _) => {
unreachable!("unexpected subcommand: {}", maybe_shortlink);
}
},
_ => unreachable!(),
_ => commands::root::run(&matches),
};

match res {
Expand Down
7 changes: 3 additions & 4 deletions backpack/src/bin/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use anyhow::Context;
use anyhow::Result as AnyResult;
use backpack::config::Project;
use backpack::data::CopyMode;
use backpack::git::GitCmd;
use backpack::git::GitProvider;
use backpack::{config::Config, ui::Prompt};
use clap::{Arg, ArgMatches, Command};

pub fn command() -> Command<'static> {
Command::new("add")
.about("Add a repo as a project")
.about("Save a repo as a project")
.arg(
Arg::new("git")
.short('g')
.long("git")
.help("prefer a git url")
.help("Prefer a git url")
.takes_value(false),
)
.arg(Arg::new("repo"))
Expand All @@ -28,7 +27,7 @@ pub fn run(_matches: &ArgMatches, subcommand_matches: &ArgMatches) -> AnyResult<
let (config, _) = Config::load_or_default().context("could not load configuration")?;

let prompt = &mut Prompt::build(&config, false, None);
prompt.show_projects(&CopyMode::All);
prompt.show_projects(None);
let name = prompt.ask_for_project_name(&repo)?;

// add it to the configuration and save
Expand Down
72 changes: 0 additions & 72 deletions backpack/src/bin/commands/apply.rs

This file was deleted.

6 changes: 3 additions & 3 deletions backpack/src/bin/commands/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use std::fs;

pub fn command() -> Command<'static> {
Command::new("cache")
.about("cache handling")
.about("Cache handling")
.arg(
Arg::new("rm")
.long("rm")
.help("remove the cache")
.help("Remove the cache")
.takes_value(false),
)
.arg(
Arg::new("path")
.long("path")
.help("show where the cache is stored")
.help("Show where the cache is stored")
.takes_value(false),
)
}
Expand Down
6 changes: 3 additions & 3 deletions backpack/src/bin/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ use std::path::Path;

pub fn command() -> Command<'static> {
Command::new("config")
.about("create custom configuration")
.about("Create a personal configuration")
.arg(
Arg::new("init")
.short('i')
.long("init")
.help("initialize an empty configuration file")
.help("Initialize an empty configuration file")
.takes_value(false),
)
.arg(
Arg::new("local")
.long("local")
.help("initialize local configuration")
.help("Initialize local configuration")
.takes_value(false),
)
}
Expand Down
Loading

0 comments on commit 797182f

Please sign in to comment.