Skip to content

Commit

Permalink
add --preset base (#17)
Browse files Browse the repository at this point in the history
* pc autoupdate

* add --preset base

* clean

* clean

* clean
  • Loading branch information
ysawa0 authored May 12, 2024
1 parent 0105767 commit a612bad
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 101 deletions.
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ name: CI

on:
pull_request: # Start the job on all PRs
branches: [master, main]
types: [synchronize, opened, reopened, ready_for_review]
push: # Start the job on all main branch push
branches: [master, main]

jobs:
precommit:
Expand Down
20 changes: 10 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
# Misc
#############################################################################
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-merge-conflict # Searches for merge conflict markers within files.
- id: check-added-large-files # Blocks commits that add large files. Default limit is 500kB.
Expand All @@ -24,7 +24,7 @@ repos:
# JSON, TOML
#############################################################################
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-json # Validates JSON files to ensure they are properly formatted and syntactically correct.
types: [json]
Expand All @@ -46,15 +46,15 @@ repos:
# Python
#############################################################################
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.1
rev: v2.3.1
hooks:
- id: autoflake # Removes unused imports and unused variables from Python code.
args:
- --in-place
- --remove-all-unused-imports

- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort # Sorts Python imports into sections and by alphabetical order.
args:
Expand All @@ -64,7 +64,7 @@ repos:
- python

- repo: https://github.com/psf/black
rev: 23.10.1
rev: 24.4.2
hooks:
- id: black # Formats Python code to conform to the Black code style.
args:
Expand All @@ -75,11 +75,11 @@ repos:
exclude: templates/

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8 # Lints Python code for errors and code style issues based on PEP8.
args:
- --config=.cpa/flake8.cfg
- --config=.ci/flake8.cfg
types:
- python
exclude: templates/
Expand All @@ -99,11 +99,11 @@ repos:
# CSS, Markdown, JavaScript, TypeScript, YAML style formatter
#############################################################################
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
rev: v4.0.0-alpha.8
hooks:
- id: prettier # An opinionated code formatter supporting multiple languages.
name: prettier
args: [--config, .cpa/prettier.json, --write]
args: [--config, .ci/prettier.json, --write]
types_or:
- css
- scss
Expand All @@ -112,7 +112,7 @@ repos:
- javascript
- yaml
- markdown
exclude: templates/.pre-commit-config.yaml|templates/.github/workflows/ci.yaml
exclude: templates/.pre-commit-config.yaml|templates/.github/workflows/ci.yaml|templates/base/ci.yaml

#############################################################################
# Rust
Expand Down
4 changes: 2 additions & 2 deletions example/python/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ repos:
hooks:
- id: flake8 # Lints Python code for errors and code style issues based on PEP8.
args:
- --config=.cpa/flake8.cfg
- --config=.ci/flake8.cfg
types:
- python

Expand All @@ -100,7 +100,7 @@ repos:
hooks:
- id: prettier # An opinionated code formatter supporting multiple languages.
name: prettier
args: [--config, .cpa/prettier.json, --write]
args: [--config, .ci/prettier.json, --write]
types_or:
- css
- scss
Expand Down
2 changes: 1 addition & 1 deletion example/python/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter"
},
"flake8.args": ["--config=.cpa/flake8.cfg"],
"flake8.args": ["--config=.ci/flake8.cfg"],
"files.insertFinalNewline": true
}
2 changes: 1 addition & 1 deletion example/rust/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ repos:
hooks:
- id: prettier # An opinionated code formatter supporting multiple languages.
name: prettier
args: [--config, .cpa/prettier.json, --write]
args: [--config, .ci/prettier.json, --write]
types_or:
- css
- scss
Expand Down
2 changes: 1 addition & 1 deletion example/rust/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter"
},
"flake8.args": ["--config=.cpa/flake8.cfg"],
"flake8.args": ["--config=.ci/flake8.cfg"],
"files.insertFinalNewline": true
}
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod presets;
use std::process;

use clap::Parser;
use presets::{common, python, rust};
use presets::{base, common, python, rust};
use regex::Regex;

#[derive(Parser)]
Expand Down Expand Up @@ -46,6 +46,13 @@ pub struct Language {

#[allow(clippy::needless_return)]
fn validate_preset(preset: &str) -> Language {
if preset == "base" {
return Language {
language: "base".to_string(),
ver: "".to_string(),
};
}

if preset == "rust" {
return Language {
language: "rust".to_string(),
Expand Down Expand Up @@ -78,6 +85,8 @@ fn main() {
} else if lang.language == "rust" {
let prefix = common(&args.name, create, &lang);
rust(&args.name, &prefix);
} else if lang.language == "base" {
let _prefix = base(&args.name, create, &lang);
} else {
eprintln!("Preset: {:?} not supported", args.preset);
}
Expand All @@ -93,6 +102,8 @@ fn main() {
} else if lang.language == "rust" {
let prefix = common(&args.name, create, &lang);
rust(&args.name, &prefix);
} else if lang.language == "base" {
let _prefix = base(&args.name, create, &lang);
} else {
eprintln!("Preset: {:?} not supported", args.preset);
}
Expand Down
39 changes: 29 additions & 10 deletions src/presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ pub struct Makefile {}
pub struct GhCI {}

#[derive(Template)]
#[template(path = ".cpa/prettier.json", escape = "none")]
#[template(path = "base/ci.yaml", escape = "none")]
pub struct GhCIBase {}

#[derive(Template)]
#[template(path = ".ci/prettier.json", escape = "none")]
pub struct Prettier {}

#[derive(Template)]
Expand All @@ -54,17 +58,17 @@ pub struct PreCommitConfig {
pub language: String,
}

#[derive(Template)]
#[template(path = "base/.pre-commit-config.yaml", escape = "none")]
pub struct PreCommitConfigBase {}

////////////////////////////////////
// PYTHON
////////////////////////////////////
#[derive(Template)]
#[template(path = "python/Dockerfile", escape = "none")]
pub struct PyDockerfile {}

#[derive(Template)]
#[template(path = "python/main.py", escape = "none")]
pub struct PyMain {}

#[derive(Template)]
#[template(path = "python/pyproject.toml", escape = "none")]
pub struct PyProject {
Expand All @@ -74,7 +78,7 @@ pub struct PyProject {
}

#[derive(Template)]
#[template(path = ".cpa/flake8.cfg", escape = "none")]
#[template(path = ".ci/flake8.cfg", escape = "none")]
pub struct Flake8 {}

////////////////////////////////////
Expand All @@ -98,7 +102,7 @@ pub fn common(name: &str, create: bool, lang: &Language) -> String {
let prefix: String = if create { format!("./{}", name) } else { "./".to_string() };

// Create needed dirs
let _ = fs::create_dir_all(format!("{}/.cpa", prefix));
let _ = fs::create_dir_all(format!("{}/.ci", prefix));
let _ = fs::create_dir_all(format!("{}/.vscode", prefix));
let _ = fs::create_dir_all(format!("{}/.github/workflows", prefix));

Expand All @@ -110,7 +114,7 @@ pub fn common(name: &str, create: bool, lang: &Language) -> String {
language: lang.language.to_string(),
}
.write(&prefix, ".pre-commit-config.yaml");
Prettier {}.write(&prefix, ".cpa/prettier.json");
Prettier {}.write(&prefix, ".ci/prettier.json");
VSCodeSettings {}.write(&prefix, ".vscode/settings.json");
VSCodeExtensions {}.write(&prefix, ".vscode/extensions.json");
prefix
Expand All @@ -120,8 +124,7 @@ pub fn python(name: &str, prefix: &str, lang: &Language) {
let black_target_ver = format!("py{}", lang.ver.replace('.', ""));

// Render Python-specific files
Flake8 {}.write(prefix, ".cpa/flake8.cfg");
PyMain {}.write(prefix, "main.py");
Flake8 {}.write(prefix, ".ci/flake8.cfg");
PyDockerfile {}.write(prefix, "Dockerfile");

let pyproj: PyProject = PyProject {
Expand All @@ -140,3 +143,19 @@ pub fn rust(name: &str, prefix: &str) {
CargoToml { name: name.to_string() }.write(prefix, "Cargo.toml");
RustFmt {}.write(prefix, "rustfmt.toml");
}

pub fn base(name: &str, create: bool, _lang: &Language) -> String {
let prefix: String = if create { format!("./{}", name) } else { "./".to_string() };

// Create needed dirs
let _ = fs::create_dir_all(format!("{}/.ci", prefix));
let _ = fs::create_dir_all(format!("{}/.github/workflows", prefix));

// Render common files
GhCIBase {}.write(&prefix, ".github/workflows/ci.yaml");
GitIgnore {}.write(&prefix, ".gitignore");
Makefile {}.write(&prefix, "Makefile");
PreCommitConfigBase {}.write(&prefix, ".pre-commit-config.yaml");
Prettier {}.write(&prefix, ".ci/prettier.json");
prefix
}
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions templates/.github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ name: CI

on:
pull_request: # Start the job on all PRs
branches: [master, main]
types: [synchronize, opened, reopened, ready_for_review]
push: # Start the job on all main branch push
branches: [master, main]

jobs:
precommit:
Expand Down
4 changes: 2 additions & 2 deletions templates/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ repos:
hooks:
- id: flake8 # Lints Python code for errors and code style issues based on PEP8.
args:
- --config=.cpa/flake8.cfg
- --config=.ci/flake8.cfg
types:
- python

Expand Down Expand Up @@ -110,7 +110,7 @@ repos:
hooks:
- id: prettier # An opinionated code formatter supporting multiple languages.
name: prettier
args: [--config, .cpa/prettier.json, --write]
args: [--config, .ci/prettier.json, --write]
types_or:
- css
- scss
Expand Down
2 changes: 1 addition & 1 deletion templates/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter"
},
"flake8.args": ["--config=.cpa/flake8.cfg"],
"flake8.args": ["--config=.ci/flake8.cfg"],
"files.insertFinalNewline": true
}
52 changes: 52 additions & 0 deletions templates/base/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
default_language_version:
python: python3

repos:
#############################################################################
# Misc
#############################################################################
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-merge-conflict # Searches for merge conflict markers within files.
- id: check-added-large-files # Blocks commits that add large files. Default limit is 500kB.
# Can be configured with args, e.g., '--maxkb=1000' to change the limit.
# exclude: 'your_dir/.*'
# args: ['--maxkb=5000']
- id: check-case-conflict # Identifies potential case-insensitive file name conflicts.
- id: check-ast # Validates the syntax of Python files.
- id: check-symlinks # Detects broken symlinks.
- id: trailing-whitespace # Removes any trailing whitespace at the end of lines.
- id: end-of-file-fixer # Ensures files end with a single newline or are empty.

#############################################################################
# JSON, TOML
#############################################################################
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-json # Validates JSON files to ensure they are properly formatted and syntactically correct.
types: [json]

#############################################################################
# Shell
#############################################################################
- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 3.0.0
hooks:
- id: shfmt # Formats shell scripts to a standard convention using shfmt.
- id: shellcheck # Lints shell scripts to identify syntax and usage errors, with a specified severity of 'warning'.
args:
- --severity=warning
#############################################################################
# CSS, Markdown, JavaScript, TypeScript, YAML style formatter
#############################################################################
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
hooks:
- id: prettier # An opinionated code formatter supporting multiple languages.
name: prettier
args: [--config, .ci/prettier.json, --write]
types_or:
- yaml
- markdown
Loading

0 comments on commit a612bad

Please sign in to comment.