Skip to content

Commit e946432

Browse files
committed
add --preset base
1 parent 0bd8467 commit e946432

15 files changed

+193
-89
lines changed

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ repos:
7979
hooks:
8080
- id: flake8 # Lints Python code for errors and code style issues based on PEP8.
8181
args:
82-
- --config=.cpa/flake8.cfg
82+
- --config=.ci/flake8.cfg
8383
types:
8484
- python
8585
exclude: templates/
@@ -103,7 +103,7 @@ repos:
103103
hooks:
104104
- id: prettier # An opinionated code formatter supporting multiple languages.
105105
name: prettier
106-
args: [--config, .cpa/prettier.json, --write]
106+
args: [--config, .ci/prettier.json, --write]
107107
types_or:
108108
- css
109109
- scss
@@ -112,7 +112,7 @@ repos:
112112
- javascript
113113
- yaml
114114
- markdown
115-
exclude: templates/.pre-commit-config.yaml|templates/.github/workflows/ci.yaml
115+
exclude: templates/.pre-commit-config.yaml|templates/.github/workflows/ci.yaml|templates/base/ci.yaml
116116

117117
#############################################################################
118118
# Rust

example/python/.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ repos:
7777
hooks:
7878
- id: flake8 # Lints Python code for errors and code style issues based on PEP8.
7979
args:
80-
- --config=.cpa/flake8.cfg
80+
- --config=.ci/flake8.cfg
8181
types:
8282
- python
8383

@@ -100,7 +100,7 @@ repos:
100100
hooks:
101101
- id: prettier # An opinionated code formatter supporting multiple languages.
102102
name: prettier
103-
args: [--config, .cpa/prettier.json, --write]
103+
args: [--config, .ci/prettier.json, --write]
104104
types_or:
105105
- css
106106
- scss

example/python/.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"editor.formatOnSave": true,
55
"editor.defaultFormatter": "ms-python.black-formatter"
66
},
7-
"flake8.args": ["--config=.cpa/flake8.cfg"],
7+
"flake8.args": ["--config=.ci/flake8.cfg"],
88
"files.insertFinalNewline": true
99
}

example/rust/.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ repos:
5959
hooks:
6060
- id: prettier # An opinionated code formatter supporting multiple languages.
6161
name: prettier
62-
args: [--config, .cpa/prettier.json, --write]
62+
args: [--config, .ci/prettier.json, --write]
6363
types_or:
6464
- css
6565
- scss

example/rust/.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"editor.formatOnSave": true,
55
"editor.defaultFormatter": "ms-python.black-formatter"
66
},
7-
"flake8.args": ["--config=.cpa/flake8.cfg"],
7+
"flake8.args": ["--config=.ci/flake8.cfg"],
88
"files.insertFinalNewline": true
99
}

src/main.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod presets;
33
use std::process;
44

55
use clap::Parser;
6-
use presets::{common, python, rust};
6+
use presets::{base, common, python, rust};
77
use regex::Regex;
88

99
#[derive(Parser)]
@@ -46,6 +46,13 @@ pub struct Language {
4646

4747
#[allow(clippy::needless_return)]
4848
fn validate_preset(preset: &str) -> Language {
49+
if preset == "base" {
50+
return Language {
51+
language: "base".to_string(),
52+
ver: "".to_string(),
53+
};
54+
}
55+
4956
if preset == "rust" {
5057
return Language {
5158
language: "rust".to_string(),
@@ -78,6 +85,8 @@ fn main() {
7885
} else if lang.language == "rust" {
7986
let prefix = common(&args.name, create, &lang);
8087
rust(&args.name, &prefix);
88+
} else if lang.language == "base" {
89+
let _prefix = base(&args.name, create, &lang);
8190
} else {
8291
eprintln!("Preset: {:?} not supported", args.preset);
8392
}
@@ -93,6 +102,8 @@ fn main() {
93102
} else if lang.language == "rust" {
94103
let prefix = common(&args.name, create, &lang);
95104
rust(&args.name, &prefix);
105+
} else if lang.language == "base" {
106+
let _prefix = base(&args.name, create, &lang);
96107
} else {
97108
eprintln!("Preset: {:?} not supported", args.preset);
98109
}

src/presets.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ pub struct Makefile {}
4545
pub struct GhCI {}
4646

4747
#[derive(Template)]
48-
#[template(path = ".cpa/prettier.json", escape = "none")]
48+
#[template(path = "base/ci.yaml", escape = "none")]
49+
pub struct GhCIBase {}
50+
51+
#[derive(Template)]
52+
#[template(path = ".ci/prettier.json", escape = "none")]
4953
pub struct Prettier {}
5054

5155
#[derive(Template)]
@@ -54,17 +58,19 @@ pub struct PreCommitConfig {
5458
pub language: String,
5559
}
5660

61+
#[derive(Template)]
62+
#[template(path = "base/.pre-commit-config.yaml", escape = "none")]
63+
pub struct PreCommitConfigBase {
64+
pub language: String,
65+
}
66+
5767
////////////////////////////////////
5868
// PYTHON
5969
////////////////////////////////////
6070
#[derive(Template)]
6171
#[template(path = "python/Dockerfile", escape = "none")]
6272
pub struct PyDockerfile {}
6373

64-
#[derive(Template)]
65-
#[template(path = "python/main.py", escape = "none")]
66-
pub struct PyMain {}
67-
6874
#[derive(Template)]
6975
#[template(path = "python/pyproject.toml", escape = "none")]
7076
pub struct PyProject {
@@ -74,7 +80,7 @@ pub struct PyProject {
7480
}
7581

7682
#[derive(Template)]
77-
#[template(path = ".cpa/flake8.cfg", escape = "none")]
83+
#[template(path = ".ci/flake8.cfg", escape = "none")]
7884
pub struct Flake8 {}
7985

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

100106
// Create needed dirs
101-
let _ = fs::create_dir_all(format!("{}/.cpa", prefix));
107+
let _ = fs::create_dir_all(format!("{}/.ci", prefix));
102108
let _ = fs::create_dir_all(format!("{}/.vscode", prefix));
103109
let _ = fs::create_dir_all(format!("{}/.github/workflows", prefix));
104110

@@ -110,7 +116,7 @@ pub fn common(name: &str, create: bool, lang: &Language) -> String {
110116
language: lang.language.to_string(),
111117
}
112118
.write(&prefix, ".pre-commit-config.yaml");
113-
Prettier {}.write(&prefix, ".cpa/prettier.json");
119+
Prettier {}.write(&prefix, ".ci/prettier.json");
114120
VSCodeSettings {}.write(&prefix, ".vscode/settings.json");
115121
VSCodeExtensions {}.write(&prefix, ".vscode/extensions.json");
116122
prefix
@@ -120,8 +126,7 @@ pub fn python(name: &str, prefix: &str, lang: &Language) {
120126
let black_target_ver = format!("py{}", lang.ver.replace('.', ""));
121127

122128
// Render Python-specific files
123-
Flake8 {}.write(prefix, ".cpa/flake8.cfg");
124-
PyMain {}.write(prefix, "main.py");
129+
Flake8 {}.write(prefix, ".ci/flake8.cfg");
125130
PyDockerfile {}.write(prefix, "Dockerfile");
126131

127132
let pyproj: PyProject = PyProject {
@@ -140,3 +145,22 @@ pub fn rust(name: &str, prefix: &str) {
140145
CargoToml { name: name.to_string() }.write(prefix, "Cargo.toml");
141146
RustFmt {}.write(prefix, "rustfmt.toml");
142147
}
148+
149+
pub fn base(name: &str, create: bool, lang: &Language) -> String {
150+
let prefix: String = if create { format!("./{}", name) } else { "./".to_string() };
151+
152+
// Create needed dirs
153+
let _ = fs::create_dir_all(format!("{}/.ci", prefix));
154+
let _ = fs::create_dir_all(format!("{}/.github/workflows", prefix));
155+
156+
// Render common files
157+
GhCIBase {}.write(&prefix, ".github/workflows/ci.yaml");
158+
GitIgnore {}.write(&prefix, ".gitignore");
159+
Makefile {}.write(&prefix, "Makefile");
160+
PreCommitConfigBase {
161+
language: lang.language.to_string(),
162+
}
163+
.write(&prefix, ".pre-commit-config.yaml");
164+
Prettier {}.write(&prefix, ".ci/prettier.json");
165+
prefix
166+
}
File renamed without changes.
File renamed without changes.

templates/.github/workflows/ci.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ name: CI
33

44
on:
55
pull_request: # Start the job on all PRs
6-
branches: [master, main]
7-
types: [synchronize, opened, reopened, ready_for_review]
8-
push: # Start the job on all main branch push
9-
branches: [master, main]
106

117
jobs:
128
precommit:

templates/.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ repos:
7777
hooks:
7878
- id: flake8 # Lints Python code for errors and code style issues based on PEP8.
7979
args:
80-
- --config=.cpa/flake8.cfg
80+
- --config=.ci/flake8.cfg
8181
types:
8282
- python
8383

@@ -110,7 +110,7 @@ repos:
110110
hooks:
111111
- id: prettier # An opinionated code formatter supporting multiple languages.
112112
name: prettier
113-
args: [--config, .cpa/prettier.json, --write]
113+
args: [--config, .ci/prettier.json, --write]
114114
types_or:
115115
- css
116116
- scss

templates/.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"editor.formatOnSave": true,
55
"editor.defaultFormatter": "ms-python.black-formatter"
66
},
7-
"flake8.args": ["--config=.cpa/flake8.cfg"],
7+
"flake8.args": ["--config=.ci/flake8.cfg"],
88
"files.insertFinalNewline": true
99
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
default_language_version:
2+
python: python3
3+
4+
repos:
5+
#############################################################################
6+
# Misc
7+
#############################################################################
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v4.5.0
10+
hooks:
11+
- id: check-merge-conflict # Searches for merge conflict markers within files.
12+
- id: check-added-large-files # Blocks commits that add large files. Default limit is 500kB.
13+
# Can be configured with args, e.g., '--maxkb=1000' to change the limit.
14+
# exclude: 'your_dir/.*'
15+
# args: ['--maxkb=5000']
16+
- id: check-case-conflict # Identifies potential case-insensitive file name conflicts.
17+
- id: check-ast # Validates the syntax of Python files.
18+
- id: check-symlinks # Detects broken symlinks.
19+
- id: trailing-whitespace # Removes any trailing whitespace at the end of lines.
20+
- id: end-of-file-fixer # Ensures files end with a single newline or are empty.
21+
22+
#############################################################################
23+
# JSON, TOML
24+
#############################################################################
25+
- repo: https://github.com/pre-commit/pre-commit-hooks
26+
rev: v4.5.0
27+
hooks:
28+
- id: check-json # Validates JSON files to ensure they are properly formatted and syntactically correct.
29+
types: [json]
30+
31+
#############################################################################
32+
# Shell
33+
#############################################################################
34+
- repo: https://github.com/jumanjihouse/pre-commit-hooks
35+
rev: 3.0.0
36+
hooks:
37+
- id: shfmt # Formats shell scripts to a standard convention using shfmt.
38+
- id: shellcheck # Lints shell scripts to identify syntax and usage errors, with a specified severity of 'warning'.
39+
args:
40+
- --severity=warning
41+
#############################################################################
42+
# CSS, Markdown, JavaScript, TypeScript, YAML style formatter
43+
#############################################################################
44+
- repo: https://github.com/pre-commit/mirrors-prettier
45+
rev: v3.0.3
46+
hooks:
47+
- id: prettier # An opinionated code formatter supporting multiple languages.
48+
name: prettier
49+
args: [--config, .ci/prettier.json, --write]
50+
types_or:
51+
- yaml
52+
- markdown

templates/base/ci.yaml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{% raw -%}
2+
name: CI
3+
4+
on:
5+
pull_request: # Start the job on all PRs
6+
7+
jobs:
8+
precommit:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: "3.10"
19+
20+
- name: Set shfmt version environment variable
21+
run: echo "SHFMT_VERSION=v3.7.0" >> $GITHUB_ENV
22+
23+
- name: Cache pip dependencies
24+
uses: actions/cache@v3
25+
with:
26+
path: ~/.cache/pip
27+
key: ${{ runner.os }}-pip-
28+
restore-keys: |
29+
${{ runner.os }}-pip-
30+
31+
- name: Cache shfmt binary
32+
uses: actions/cache@v3
33+
with:
34+
path: /usr/local/bin/shfmt
35+
key: ${{ runner.os }}-shfmt-${{ env.SHFMT_VERSION }}
36+
restore-keys: |
37+
${{ runner.os }}-shfmt-${{ env.SHFMT_VERSION }}
38+
${{ runner.os }}-shfmt-
39+
40+
- name: Cache Pre-Commit environments
41+
uses: actions/cache@v3
42+
with:
43+
path: ~/.cache/pre-commit
44+
key: ${{ runner.os }}-pc-${{ hashFiles('.pre-commit-config.yaml') }}
45+
restore-keys: |
46+
${{ runner.os }}-pc-${{ hashFiles('.pre-commit-config.yaml') }}
47+
${{ runner.os }}-pc-
48+
49+
- name: Install dependencies
50+
run: |
51+
python -m pip install pre-commit
52+
pre-commit install
53+
54+
- name: Install shfmt
55+
run: |
56+
SHFMT_VERSION=${{ env.SHFMT_VERSION }}
57+
SHFMT_BIN="shfmt_${SHFMT_VERSION}_linux_amd64"
58+
if [[ ! -f /usr/local/bin/shfmt ]]; then
59+
wget -O shfmt "https://github.com/mvdan/sh/releases/download/${SHFMT_VERSION}/${SHFMT_BIN}"
60+
chmod +x shfmt
61+
sudo mv shfmt /usr/local/bin/
62+
fi
63+
sudo apt-get install shellcheck
64+
65+
- name: Run pre-commits
66+
env:
67+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68+
run: |
69+
REPO_NAME=$(echo $GITHUB_REPOSITORY | sed 's/^.*\///')
70+
DEFAULT_BRANCH=$(curl -H "Authorization: token $GITHUB_TOKEN" \
71+
"https://api.github.com/repos/$GITHUB_REPOSITORY" | jq -r '.default_branch')
72+
73+
git fetch
74+
CUR_SHA=$(git log --pretty=tformat:"%H" -n1 . | tail -n1)
75+
76+
echo "Default branch is $DEFAULT_BRANCH"
77+
echo "Current SHA is $CUR_SHA"
78+
79+
if [[ $GITHUB_REF == "refs/heads/$DEFAULT_BRANCH" ]]; then
80+
pre-commit run --all
81+
else
82+
pre-commit run --from-ref origin/$DEFAULT_BRANCH --to-ref $CUR_SHA
83+
fi
84+
{%- endraw %}

0 commit comments

Comments
 (0)