Skip to content

Commit

Permalink
➕ Add git repo support
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Apr 18, 2024
1 parent c5a7486 commit 0dc562f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ rhai = { version = "1.17.1", features = [
"no_module",
"no_custom_syntax"
] }
git2 = { version = "0.18.3", optional = true }

[features]
# See README.md for descriptions about the features.
default = ["export", "manual", "share", "watch"]
default = ["export", "git", "manual", "share", "watch"]
config-schema = ["schemars"]
export = ["open"]
git = ["git2"]
manual = ["open"]
share = ["tokio", "warp", "qrcode", "local-ip-address"]
watch = ["notify", "notify-debouncer-mini"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Feature | Flag | Description | Enabl
--------------------|------------------|---------------------------------|-----------------------
**share command** | `share` | Shares add-ons over HTTP | yes
**export command** | `export` | Exports add-ons to Minecraft | yes
**git** | `git` | Handles `git` | yes
**schema command** | `config-schema` | JSON schema for config file | no
**watch command** | `watch` | Rebuild add-ons on file changes | yes
**manual command** | `manual` | Opens the manual | yes
Expand Down
19 changes: 17 additions & 2 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub fn cmd() -> Command {
.value_parser(clap::value_parser!(PathBuf))
.default_value("."),
)
.arg(
Arg::new("init-git")
.long("no-git")
.help("Prevents creation of a git repository")
.action(ArgAction::SetFalse),
)
.arg(
Arg::new("gitignore")
.long("no-gitignore")
Expand All @@ -31,9 +37,18 @@ pub fn run(matches: &ArgMatches) -> ExitCode {
let path: &PathBuf = matches.get_one("dir").unwrap();
let force: bool = matches.get_flag("force");
let with_gitignore: bool = matches.get_flag("gitignore");
let init_git: bool = matches.get_flag("init-git");

Project::new(path, force, ProjectInitConfig { with_gitignore })
.expect("Failed do initialize project");
Project::new(
path,
force,
ProjectInitConfig {
with_gitignore,
#[cfg(feature = "git")]
init_git,
},
)
.expect("Failed do initialize project");

ExitCode::SUCCESS
}
13 changes: 12 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ use zip_extensions::write::zip_create_from_directory;

#[derive(Clone, Copy, Debug)]
pub struct ProjectInitConfig {
/// Whether to generate a `.gitignore` file.
pub with_gitignore: bool,

#[cfg(feature = "git")]
/// Whether to initialite a new git repository.
pub init_git: bool,
}

#[derive(Clone)]
Expand All @@ -36,7 +41,6 @@ pub struct Project {

impl Project {
pub fn new(dir: &PathBuf, force: bool, config: ProjectInitConfig) -> Result<Self, io::Error> {
// TODO: initialize new git repo
let empty = dir.read_dir()?.count().eq(&0);
if !empty && !force {
return Err(io::Error::new(
Expand All @@ -62,6 +66,13 @@ impl Project {
fs::write(dir.join(paths::uuids()), uuids.to_string())?;
fs::write(dir.join(paths::version()), clap::crate_version!())?;
fs::write(dir.join(paths::project_id()), id.to_string())?;
#[cfg(feature = "git")]
{
match git2::Repository::init(&dir) {
Ok(_repo) => {}
Err(e) => log::error!("Failed to initialize git repository: {}", e),
};
};
if config.with_gitignore {
fs::write(dir.join(paths::gitignore()), scaffolding::GITIGNORE)?;
}
Expand Down

0 comments on commit 0dc562f

Please sign in to comment.