Skip to content

Commit

Permalink
Fix the inheritance overwrites for multi-level inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
tmattio committed Jul 31, 2021
1 parent dc63ade commit 7fc6742
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.8.3

## Fixed

- Fixed the inheritance overwrites for multi-level inheritance

# 0.8.2

## Fixed
Expand Down
73 changes: 58 additions & 15 deletions lib/spin/template.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ let read_source_template_files ?(download_git = false) source =
let rec of_dec
?(use_defaults = false)
?(files = Hashtbl.create 256)
?(ignore_configs = false)
?(ignore_actions = false)
?(ignore_example_commands = false)
?ignore_configs
?ignore_actions
?ignore_example_commands
~source
~context
~depth
(dec : Dec_template.t)
=
let open Result.Syntax in
Expand All @@ -161,13 +162,25 @@ let rec of_dec
|> Result.map_error (fun reason ->
Spin_error.invalid_template ~msg:reason dec.name)
in
read
let ignore_configs =
Option.value ignore_configs ~default:base.ignore_configs
in
let ignore_actions =
Option.value ignore_actions ~default:base.ignore_actions
in
let ignore_example_commands =
Option.value
ignore_example_commands
~default:base.ignore_example_commands
in
read_template
source
~use_defaults
~context
~ignore_configs:base.ignore_configs
~ignore_actions:base.ignore_actions
~ignore_example_commands:base.ignore_example_commands
~ignore_configs
~ignore_actions
~ignore_example_commands
~depth:(depth + 1)
| None ->
Result.ok
{ name = ""
Expand All @@ -182,6 +195,29 @@ let rec of_dec
; source
}
in
let compute_ignore v f =
match depth, v, dec.base_template with
| 0, _, _ ->
false
| _, Some x, _ ->
x
| _, None, Some base_template ->
f base_template
| _, None, None ->
false
in
let ignore_configs =
compute_ignore ignore_configs (fun x ->
x.Dec_template.Base_template.ignore_configs)
in
let ignore_actions =
compute_ignore ignore_actions (fun x ->
x.Dec_template.Base_template.ignore_actions)
in
let ignore_example_commands =
compute_ignore ignore_example_commands (fun x ->
x.Dec_template.Base_template.ignore_example_commands)
in
let* () =
if ignore_configs then
Result.ok ()
Expand Down Expand Up @@ -235,27 +271,34 @@ let rec of_dec
; source
}

and read
and read_template
?(use_defaults = false)
?(ignore_configs = false)
?(ignore_actions = false)
?(ignore_example_commands = false)
?ignore_configs
?ignore_actions
?ignore_example_commands
?context
~depth
source
=
let open Result.Syntax in
let context = Option.value context ~default:(Hashtbl.create 256) in
let* spin_file = read_source_spin_file source ~download_git:true in
let* (spin_file : Dec_template.t) =
read_source_spin_file source ~download_git:true
in
let* files = read_source_template_files source ~download_git:false in
of_dec
spin_file
~ignore_configs
~ignore_actions
~ignore_example_commands
?ignore_configs
?ignore_actions
?ignore_example_commands
~files
~context
~source
~use_defaults
~depth

let read ?(use_defaults = false) ?context source =
read_template ~use_defaults ?context ~depth:0 source

let run_actions ~path actions =
let open Result.Syntax in
Expand Down
14 changes: 0 additions & 14 deletions lib/spin/template.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,8 @@ val read_source_template_files
-> source
-> ((string, string) Hashtbl.t, Spin_error.t) Result.t

val of_dec
: ?use_defaults:bool
-> ?files:(string, string) Hashtbl.t
-> ?ignore_configs:bool
-> ?ignore_actions:bool
-> ?ignore_example_commands:bool
-> source:source
-> context:(string, string) Hashtbl.t
-> Dec_template.t
-> (t, Spin_error.t) Result.t

val read
: ?use_defaults:bool
-> ?ignore_configs:bool
-> ?ignore_actions:bool
-> ?ignore_example_commands:bool
-> ?context:(string, string) Hashtbl.t
-> source
-> (t, Spin_error.t) Result.t
Expand Down
8 changes: 8 additions & 0 deletions test_bin/success/inherit-multi-override.t/base/spin
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(name base)
(description "Test template")

(example_commands
(commands
("make deps" "Download runtime and development dependencies.")
("make build" "Build the dependencies and the project.")
("make test" "Starts the test runner.")))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Empty file.
22 changes: 22 additions & 0 deletions test_bin/success/inherit-multi-override.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$ spin new --ignore-config subdir-2 _generated

🏗️ Creating a new project from test in _generated
Done!

🎉 Success! Your project is ready at _generated

Here are some example commands that you can run inside this directory:

make build
Build the dependencies and the project.

make test
Starts the test runner.

Happy hacking!


$ ls _generated
base_file
new_file
new_file_2
6 changes: 6 additions & 0 deletions test_bin/success/inherit-multi-override.t/subdir-1/spin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(inherit (local base))

(name test)
(description "Test template")

(ignore (files ignored_base_file))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Empty file.
13 changes: 13 additions & 0 deletions test_bin/success/inherit-multi-override.t/subdir-2/spin
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(inherit
(local subdir-1)
(overwrites (example_commands)))

(name test)
(description "Test template")

(ignore (files new_file_to_ignore))

(example_commands
(commands
("make build" "Build the dependencies and the project.")
("make test" "Starts the test runner.")))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!

0 comments on commit 7fc6742

Please sign in to comment.