Skip to content

Commit 9ce04c5

Browse files
committed
add ci feat
1 parent f4a95b1 commit 9ce04c5

File tree

6 files changed

+259
-44
lines changed

6 files changed

+259
-44
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ repos:
109109
- javascript
110110
- yaml
111111
- markdown
112-
exclude: templates/.pre-commit-config.yaml
112+
exclude: templates/.pre-commit-config.yaml|templates/.github/workflows/ci.yaml
113113

114114
#############################################################################
115115
# Rust

example/.github/workflows/ci.yaml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: CI
2+
3+
on:
4+
pull_request: # Start the job on all PRs
5+
branches: [master, main]
6+
types: [synchronize, opened, reopened, ready_for_review]
7+
push: # Start the job on all main branch push
8+
branches: [master, main]
9+
10+
jobs:
11+
precommit:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: "3.10"
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+
key: ${{ runner.os }}-shfmt-
37+
restore-keys: |
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+
key: ${{ runner.os }}-pc-
46+
restore-keys: |
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="v3.7.0"
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

example/pyproject.toml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[tool.poetry]
2+
name = "example"
3+
version = "0.0.1"
4+
description = ""
5+
authors = [
6+
"My Name <[email protected]>"
7+
]
8+
license = ""
9+
10+
[build-system]
11+
requires = ["poetry-core>=1.7.0"]
12+
build-backend = "poetry.core.masonry.api"
13+
14+
[tool.black]
15+
line-length = 120
16+
skip-string-normalization = false
17+
target-version = ['py310']
18+
include = '\.pyi?$'
19+
exclude = '''
20+
/(
21+
\.eggs
22+
| \.git
23+
| \.hg
24+
| \.mypy_cache
25+
| \.tox
26+
| \.venv
27+
| _build
28+
| buck-out
29+
| build
30+
| dist
31+
)/
32+
'''
33+
color = true
34+
35+
[tool.isort]
36+
balanced_wrapping = true
37+
include_trailing_comma = true
38+
known_first_party = "example"
39+
known_third_party = [
40+
"boto3", # Common for AWS
41+
"django", # Common web framework, if used
42+
"flask", # Common web framework, if used
43+
"jinja2", # Common templating engine
44+
"matplotlib", # Common for plotting
45+
"numpy", # Common for numerical operations
46+
"pandas", # Common for data manipulation
47+
"pendulum", # Common for date time
48+
"pytest", # Common for testing
49+
"requests", # Common for HTTP requests
50+
"sqlalchemy", # Common ORM for databases
51+
]
52+
multi_line_output = 3
53+
profile = "black"
54+
line_length = 120
55+
56+
[tool.poetry.dependencies]
57+
python = "^3.10"
58+
59+
[tool.poetry.group.dev.dependencies]
60+
pre-commit = ">=3.5.0"
61+
pytest = "^7.3.1"
62+
pytest-cov = "^4.1.0"

rustfmt.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
max_width = 120
2+
imports_granularity = "Crate"
3+
group_imports = "StdExternalCrate"
4+
imports_layout = "Horizontal"
5+
6+
comment_width = 200

src/python.rs

+22-43
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ struct Flake8 {}
4444
#[template(path = ".cpa/prettier.json", escape = "none")]
4545
struct Prettier {}
4646

47+
#[derive(Template)]
48+
#[template(path = ".github/workflows/ci.yaml", escape = "none")]
49+
struct GHWorkflowCI {}
50+
51+
// #[derive(Template)]
52+
// #[template(path = ".github/workflows/ci.yaml", escape = "none")]
53+
// struct GHWorkflowCI {}
54+
4755
pub fn setup_preset(mut preset: String, name: String, create: bool) {
4856
if preset == "python" {
4957
preset = "python3.10".to_string();
@@ -54,40 +62,26 @@ pub fn setup_preset(mut preset: String, name: String, create: bool) {
5462
}
5563

5664
let _ = fs::create_dir_all(format!("{}/.cpa", prefix));
65+
66+
// Render Github Actions CI
67+
let _ = fs::create_dir_all(format!("{}/.github/workflows", prefix));
68+
File::create(format!("{}/.github/workflows/ci.yaml", prefix))
69+
.and_then(|mut file| file.write_all(GHWorkflowCI {}.render().expect("Failed to render ci.yaml").as_bytes()))
70+
.expect("Failed to create or write to ci.yaml");
71+
5772
// Render .gitignore
5873
File::create(format!("{}/.gitignore", prefix))
59-
.and_then(|mut file| {
60-
file.write_all(
61-
GitIgnore {}
62-
.render()
63-
.expect("Failed to render .gitignore")
64-
.as_bytes(),
65-
)
66-
})
74+
.and_then(|mut file| file.write_all(GitIgnore {}.render().expect("Failed to render .gitignore").as_bytes()))
6775
.expect("Failed to create or write to .gitignore");
6876

6977
// Render Makefile
7078
File::create(format!("{}/Makefile", prefix))
71-
.and_then(|mut file| {
72-
file.write_all(
73-
Makefile {}
74-
.render()
75-
.expect("Failed to render Makefile")
76-
.as_bytes(),
77-
)
78-
})
79+
.and_then(|mut file| file.write_all(Makefile {}.render().expect("Failed to render Makefile").as_bytes()))
7980
.expect("Failed to create or write to Makefile");
8081

8182
// Render Dockerfile
8283
File::create(format!("{}/Dockerfile", prefix))
83-
.and_then(|mut file| {
84-
file.write_all(
85-
Dockerfile {}
86-
.render()
87-
.expect("Failed to render Dockerfile")
88-
.as_bytes(),
89-
)
90-
})
84+
.and_then(|mut file| file.write_all(Dockerfile {}.render().expect("Failed to render Dockerfile").as_bytes()))
9185
.expect("Failed to create or write to Dockerfile");
9286

9387
// Render main.py
@@ -101,34 +95,20 @@ pub fn setup_preset(mut preset: String, name: String, create: bool) {
10195
file.write_all(
10296
PreCommitConfig { python: true }
10397
.render()
104-
.expect("Failed to render .pre-commit-config.yaml")
98+
.expect("Failed to render .pre-commit-config.yam")
10599
.as_bytes(),
106100
)
107101
})
108102
.expect("Failed to create or write to .pre-commit-config.yaml");
109103

110104
// Render Flake8 conf
111105
File::create(format!("{}/.cpa/flake8.cfg", prefix))
112-
.and_then(|mut file| {
113-
file.write_all(
114-
Flake8 {}
115-
.render()
116-
.expect("Failed to render flake8.cfg")
117-
.as_bytes(),
118-
)
119-
})
106+
.and_then(|mut file| file.write_all(Flake8 {}.render().expect("Failed to render flake8.cfg").as_bytes()))
120107
.expect("Failed to create or write to flake8.cfg");
121108

122109
// Render Prettier conf
123110
File::create(format!("{}/.cpa/prettier.json", prefix))
124-
.and_then(|mut file| {
125-
file.write_all(
126-
Prettier {}
127-
.render()
128-
.expect("Failed to render prettier.json")
129-
.as_bytes(),
130-
)
131-
})
111+
.and_then(|mut file| file.write_all(Prettier {}.render().expect("Failed to render prettier.json").as_bytes()))
132112
.expect("Failed to create or write to prettier.json");
133113

134114
// Render Poetry conf
@@ -147,8 +127,7 @@ pub fn setup_preset(mut preset: String, name: String, create: bool) {
147127
black_target_ver,
148128
};
149129
let out_pyproj: String = pyproj.render().expect("Failed to render");
150-
let mut f_pyproj =
151-
File::create(format!("{}/pyproject.toml", prefix)).expect("Could not create file");
130+
let mut f_pyproj = File::create(format!("{}/pyproject.toml", prefix)).expect("Could not create file");
152131
f_pyproj
153132
.write_all(out_pyproj.as_bytes())
154133
.expect("Could not write to file");

templates/.github/workflows/ci.yaml

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

0 commit comments

Comments
 (0)