Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Mar 7, 2023
1 parent 8f128a1 commit 26f235e
Show file tree
Hide file tree
Showing 23 changed files with 381 additions and 403 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Project

.backpack-test*/
todo.txt
rustwrap/dist/
## Rust
Expand Down
94 changes: 77 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ $ cd your-repo
$ bp add
```

Note that if a repo contains a `.backpack-project.yaml` file, it will be automatically used. A `.backpack-project.yaml` file is a way for the repo author to give instructions for how to make a template out of their project, here's an example:

```yaml
project:
shortlink: kriasoft/react-starter-kit
actions:
- name: "name of file"
hook: before
interaction:
kind: input
prompt: name of your app
out: file_name
swaps:
- key: README.md
val_template: "{{file_name}}"
path: .*
- key: tsconfig.json
val: ts-config.json
```
You create this file and format in your own repos to make them _backpack friendly_.
## :hammer: Create starters manually
Create a `backpack.yaml`:
Expand Down Expand Up @@ -131,6 +153,60 @@ projects:
path: "README.md"
```
Example of a fully personalize config in my `~/.backpack/backpack.yaml`, with minimal prompts (generating from [rust-starter](https://github.com/rusty-ferris-club/rust-starter)):

<details>
<summary>Show example</summary>

```yaml
projects:
jondot-rs:
shortlink: rusty-ferris-club/rust-starter
actions:
- name: project
interaction:
kind: input
prompt: project name (e.g. newtool)
out: project
hook: before
- name: description
interaction:
kind: input
prompt: description (e.g. one liner)
out: description
hook: before
swaps:
- key: __V_PROJECT_NAME__
val_template: "{{project}}"
path: ".*"
- key: __V_REPO_NAME__
val_template: "jondot/{{project}}"
path: ".*"
- key: __V_REPO_URL__
val_template: "https://github.com/jondot/{{project}}"
path: ".*"
- key: __V_PROJECT_FORMULA__
val_template: "{{project | capitalize}}"
path: ".*"
- key: __V_TAP_NAME__
val: "jondot/homebrew-tap"
path: ".*"
- key: __V_BIN_NAME__
val_template: "{{project}}"
path: ".*"
- key: __v_bin_name__
val_template: "{{project}}"
path: ".*"
- key: __V_AUTHOR__
val: "[email protected]"
path: ".*"
- key: __V_DESCRIPTION__
val_template: "{{description}}"
path: ".*"
```

</details>

## :raising_hand_woman: Configure user projects

`bp` (with no args) will automatically display a list of projects if configure those.
Expand All @@ -157,15 +233,6 @@ projects:
shortlink: rusty-ferris-club/rust-starter
```

Optionally indicate it is *only* suitable for applying into an existing folder:

```yaml
projects:
rust-starter:
shortlink: rusty-ferris-club/rust-starter
mode: apply # or new, or remove property.
```


## :rotating_light: Run actions and user input

Expand Down Expand Up @@ -259,11 +326,10 @@ How can I set up an enterprise / hosted git, or use Gitlab or others?
</b></summary>
You can use custom git vendors.

Start by generating a **project-local** configuration file:
Start by generating a configuration file:

```
$ bp config --init
wrote: .backpack.yaml.
```

Example: configure a Github Enterprise instance:
Expand All @@ -283,13 +349,7 @@ And now, you can use the `ghe:` prefix for your shortlinks:
$ 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**.

You can also generate a **global user config** by specifying:

```
$ bp config --init --global
```
</details>

<details>
Expand Down
47 changes: 33 additions & 14 deletions backpack/src/bin/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::path::Path;

use anyhow::Context;
use anyhow::Result as AnyResult;
use backpack::config::LocalProjectConfig;
use backpack::config::Project;
use backpack::config::PROJECT_CONFIG_FILE;
use backpack::git::GitCmd;
use backpack::git::GitProvider;
use backpack::{config::Config, ui::Prompt};
Expand All @@ -20,25 +24,40 @@ pub fn command() -> Command<'static> {
}

pub fn run(_matches: &ArgMatches, subcommand_matches: &ArgMatches) -> AnyResult<bool> {
let repo = subcommand_matches
.get_one::<String>("repo")
.map_or_else(|| GitCmd::default().get_local_url(), |r| Ok(r.to_string()))?;
let config = Config::load_or_default().context("could not load configuration")?;
let prompt = &mut Prompt::build(&config, false, None);

let (config, _) = Config::load_or_default().context("could not load configuration")?;
prompt.show_projects();

let prompt = &mut Prompt::build(&config, false, None);
prompt.show_projects(None);
let name = prompt.ask_for_project_name(&repo)?;
let local_project = LocalProjectConfig::from_path(&Path::new(".").join(PROJECT_CONFIG_FILE))?;
let (name, repo, new_config) = if let Some(local_project) = local_project {
prompt.say("Found local project config. Reading actions and swaps from it.");
let name = prompt.ask_for_project_name(&local_project.project.shortlink)?;
let mut config = config.clone();
if let Some(projects) = config.projects.as_mut() {
projects.insert(name.clone(), local_project.project.clone());
}
(name, local_project.project.shortlink.clone(), config)
} else {
let repo = subcommand_matches
.get_one::<String>("repo")
.map_or_else(|| GitCmd::default().get_local_url(), |r| Ok(r.to_string()))?;

// add it to the configuration and save
let mut config = config.clone();
if let Some(projects) = config.projects.as_mut() {
projects.insert(name.clone(), Project::from_link(&repo));
}
if prompt.are_you_sure(&format!("Save '{name}' ({}) to configuration?", &repo))? {
config.save()?;
let name = prompt.ask_for_project_name(&repo)?;
// add it to the configuration and save
let mut config = config.clone();
if let Some(projects) = config.projects.as_mut() {
projects.insert(name.clone(), Project::from_link(&repo));
}
(name, repo, config)
};

// save the new, modified, copy of config
if prompt.are_you_sure(&format!("Save '{name}' ({repo}) to configuration?"))? {
new_config.save()?;
prompt.say(&format!("Saved '{name}' to global config."));
}

prompt.suggest_edit(
"Would you like to add actions? (will open editor)",
Config::global_config_file()?.as_path(),
Expand Down
16 changes: 2 additions & 14 deletions backpack/src/bin/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ pub fn command() -> Command<'static> {
.help("Initialize an empty configuration file")
.takes_value(false),
)
.arg(
Arg::new("local")
.long("local")
.help("Initialize local configuration")
.takes_value(false),
)
}

fn print_path(kind: &str, path: &Path) {
Expand All @@ -31,19 +25,13 @@ fn print_path(kind: &str, path: &Path) {
}
pub fn run(_matches: &ArgMatches, subcommand_matches: &ArgMatches) -> AnyResult<bool> {
if subcommand_matches.is_present("init") {
let generated = if subcommand_matches.is_present("local") {
Config::init_local()?
} else {
Config::init_global()?
};
let generated = Config::init_global()?;
println!("wrote: {}.", generated.display());
} else {
let local = Config::local_config_file();
let global = Config::global_config_file()?;
print_path("global", global.as_path());
print_path("local", local.as_path());

let t = Config::load_or_default()?.0.to_text()?;
let t = Config::load_or_default()?.to_text()?;
println!("{t}");
}

Expand Down
Loading

0 comments on commit 26f235e

Please sign in to comment.