Skip to content

Commit 04a7e52

Browse files
authored
Merge pull request #4 from ShayNehmad/feature/generate-pre-receive-hook
Feature/generate pre receive hook
2 parents a3c17ec + 082450f commit 04a7e52

File tree

6 files changed

+151
-27
lines changed

6 files changed

+151
-27
lines changed

levels/checkers/start-here.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
#!/bin/bash
22

3-
source ./common.sh
3+
source $(dirname $0)/common.sh
44

55
read old new ref < /dev/stdin
66

77
dump_dir=$(dump-commit-to-directory $new)
88

9-
pushd dump_dir
10-
# Check file existence.
11-
if [ ! -f alice.txt ];
12-
then reject-solution "Alice is missing! Try again.";
13-
fi
9+
pushd $dump_dir
10+
# Check file existence.
11+
if [ ! -f alice.txt ];
12+
then reject-solution "Alice is missing! Try again.";
13+
fi
1414

15-
if [ ! -f bob.txt ];
16-
then reject-solution "Bob is missing! Try again.";
17-
fi
15+
if [ ! -f bob.txt ];
16+
then reject-solution "Bob is missing! Try again.";
17+
fi
1818
popd
1919

2020
git fetch --tags --quiet # get all the tags but don't show them to the user
2121
# Check how many commits the user needed - should be two (the user commit + merge commit)!
2222
commit_amount=$( git log start-here-tag..$new --oneline | wc -l )
2323
if [ $commit_amount -ne 1 ];
24-
then reject-solution "The files should have been added in a single commit, but I've found ${commit_amount} commits in the log. To reset and try again, delete your local start-here branch, checkout the original start-here branch again and try again.";
24+
then reject-solution "The files should have been added in a single commit, but I've found ${commit_amount} commits in the log. To reset and try again, delete your local start-here branch, checkout the original start-here branch again and try again.";
2525
fi
2626

2727
# We know that there's only one commit in the changes - otherwise it would have failed before.
28-
number_of_files_changed=$( git diff --stat $old $new | grep "files changed" | awk ' {print $1} ' )
28+
number_of_files_changed=$( git diff --stat $old $new | grep "files changed" | awk ' {print $1} ' )
2929
if [[ $number_of_files_changed -ne 2 ]]
30-
then bad "More than 2 files were changed! Only add alice.txt and bob.txt. Check out the original branch and try again.";
30+
then reject-solution "More than 2 files were changed! Only add alice.txt and bob.txt. Check out the original branch and try again.";
3131
fi
3232

levels/game-config.toml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
[levels]
2-
[levels.clone]
1+
[[levels]]
32
title = "clone"
43
branch = "master"
5-
solutionCheckers = []
4+
solution_checker = "echo No pushing to master. Read the README file; exit 1"
65
flags = [] # ["start-here"], but it's implicit in the readme
76

8-
[levels.start-here]
7+
[[levels]]
98
title = "start-here"
109
branch = "start-here"
11-
solutionChecker = "checkers/start-here.sh"
10+
solution_checker = "hooks/checkers/start-here.sh"
1211
flags = ["merge-1"]
1312

14-
[levels.merge-1]
13+
[[levels]]
1514
title = "merge-1"
1615
branch = "fizzling-vulture-pedial"
17-
solutionChecker = "checkers/merge-1.sh"
16+
solution_checker = "hooks/checkers/merge-1.sh"
1817
flags = ["merge-2", "log-1"]
18+
19+
[[levels]]
20+
title = "merge-2"
21+
branch = "ironish-quartzic-brahmas"
22+
solution_checker = "hooks/checkers/merge-2.sh"
23+
flags = ["merge-3"]
24+
25+
[[levels]]
26+
title = "log-1"
27+
branch = "akmite-radicle-garce"
28+
solution_checker = "echo No pushing in this stage. ; exit 1"
29+
flags = ["log-2"]
30+
31+
[[levels]]
32+
title = "log-2"
33+
branch = "alibies-listwork-homotaxy"
34+
solution_checker = "hooks/checkers/log-2.sh"
35+
flags = ["log-3"]
36+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Script puts files here by default. Let's avoid commiting them if possible
2+
output/
3+

scripts/generate-pre-receive-hook/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ edition = "2018"
88

99
[dependencies]
1010
structopt = "0.3.13"
11+
serde = { version = "1.0", features = ["derive"] }
1112
toml = "0.5"
13+
tinytemplate = "1.0.4"
14+
simple_logger = "1.6.0"
15+
log = "0.4"
16+
Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,110 @@
1+
use log;
2+
use log::{debug, info};
3+
use serde::{Deserialize, Serialize};
4+
use simple_logger;
15
use std::fs;
6+
use std::io::Write;
27
use structopt::StructOpt;
8+
use tinytemplate::TinyTemplate;
9+
use toml;
310

4-
// Search for a pattern in a file and display the lines that contain it.
511
#[derive(Debug, StructOpt)]
12+
#[structopt(about = "A script to generate the master pre-receive hook file.")]
613
struct Cli {
7-
// The path to the file to read
8-
#[structopt(parse(from_os_str))]
14+
#[structopt(parse(from_os_str), help = "Path to game config file to read")]
915
game_config_path: std::path::PathBuf,
16+
17+
#[structopt(parse(from_os_str), help = "Path to template file to read")]
18+
template_path: std::path::PathBuf,
19+
20+
#[structopt(
21+
parse(from_os_str),
22+
default_value = "output/pre-receive",
23+
help = "Path to output file (creates if doesn't exist)"
24+
)]
25+
output_path: std::path::PathBuf,
26+
27+
#[structopt(
28+
short = "v",
29+
long = "verbose",
30+
help = "Show more information about the actions taken"
31+
)]
32+
verbose: bool,
1033
}
1134

35+
#[derive(Debug, Clone, Deserialize, Serialize)]
1236
struct Level {
1337
title: String,
1438
branch: String,
15-
solution_checke: String,
39+
solution_checker: String,
1640
flags: Vec<String>,
1741
}
1842

43+
#[derive(Debug, Deserialize, Serialize)]
1944
struct GameConfig {
2045
levels: Vec<Level>,
2146
}
2247

48+
fn replace_flags_with_branch_names(game_config: &mut GameConfig) {
49+
let levels_info = game_config.levels.clone();
50+
51+
for mut level in &mut game_config.levels {
52+
let mut new_flags = Vec::new();
53+
for flag in &level.flags {
54+
debug!("level {} flag {}", level.title, flag);
55+
let mut levels_iterator = levels_info.iter();
56+
let found = levels_iterator.find(|&x| &x.title == flag);
57+
match found {
58+
Some(x) => {
59+
debug!("replacing {} with {}", flag, x.branch);
60+
new_flags.push(String::from(&x.branch));
61+
}
62+
None => {
63+
debug!("flag {} is final", flag);
64+
new_flags.push(flag.to_string());
65+
}
66+
}
67+
}
68+
level.flags = new_flags;
69+
}
70+
}
71+
2372
fn main() {
2473
let args = Cli::from_args();
25-
println!("Loading script from {:?}", args);
2674

27-
let game_config_file_contents =
28-
fs::read_to_string(args.game_config_path).expect("Couldn't read the config file!");
75+
if args.verbose {
76+
simple_logger::init_with_level(log::Level::Debug).unwrap();
77+
} else {
78+
simple_logger::init_with_level(log::Level::Info).unwrap();
79+
};
80+
81+
info!("Reading script from {:?}", args.game_config_path);
82+
let game_config_file_contents = fs::read_to_string(args.game_config_path).unwrap();
83+
84+
let mut game_config: GameConfig = toml::from_str(&game_config_file_contents).unwrap();
85+
debug!("Game config before editing: {:?}\n", game_config);
86+
87+
replace_flags_with_branch_names(&mut game_config);
88+
89+
debug!("Game config after editing: {:?}\n", game_config);
90+
91+
info!("Reading template from {:?}", args.template_path);
92+
let template_file_contents = fs::read_to_string(args.template_path).unwrap();
93+
94+
let mut tt = TinyTemplate::new();
95+
let template_name = "switch_case";
96+
tt.add_template(template_name, &template_file_contents)
97+
.unwrap();
98+
let rendered = tt.render(template_name, &game_config).unwrap();
99+
100+
debug!("########## RENDERED TEMPLATE ##########");
101+
debug!("{}\n", rendered);
102+
103+
let mut output_dir = args.output_path.clone();
104+
output_dir.pop();
105+
fs::create_dir_all(&output_dir).expect("Failed to create parent dir");
106+
let mut output_file = fs::File::create(&args.output_path).expect("Couldn't create file!");
107+
output_file.write_all(&rendered.as_bytes()).unwrap();
29108

30-
println!("{}", game_config_file_contents);
109+
info!("Wrote rendered file to {:?}", args.output_path);
31110
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
read old new ref < /dev/stdin
4+
5+
branch_name=$(echo $ref | awk 'BEGIN \{ FS = "/" } ; \{ print $NF }')
6+
7+
print_flags () \{
8+
if [[ $# -eq 1 ]]
9+
then echo "You won! The flag is "$1
10+
else
11+
echo "You won! The flags are "$@
12+
fi
13+
}
14+
15+
case $branch_name in
16+
{{ for level in levels }}{level.branch})
17+
echo $old $new $ref | {level.solution_checker} && print_flags{{ for levelflag in level.flags }} {levelflag}{{ endfor }}
18+
;;
19+
{{ endfor }}esac

0 commit comments

Comments
 (0)