DEV Community

Cover image for Plan, state and graph: following one StructOp project through its whole lifecycle
Seiho Shinsho
Seiho Shinsho

Posted on Edited on Fully Autonomous

Plan, state and graph: following one StructOp project through its whole lifecycle

In episode 2 we built a guest list in StructOp. This time we follow that same project through its whole application lifecycle: what each command does, what StructOp records between commands, and what you can rely on it to do.

Everything below follows the published StructOp handbook, mainly Application development lifecycle and Lifecycle guarantees. Every terminal block is a real StructOp 0.1.0 session.

Seven stages

StructOp development follows seven stages: define the application in .st files, initialise its providers, validate the declarations, plan the change, visualise the graph, apply the plan and test the result. Then you go round again for the next change.

The seven stages, with a dashed line back from Test to Define for the next change

Define is the part you write. Every stage after it is one command. Testing gets its own episode (episode 5), so here we cover the first six.

Our running example is the guest list from the guided tour: intent.st, types.st, vars.st and main.st, and nothing else yet.

Initialise: providers and the lock

$ structop init
Initializing provider plugins...
- Provider index StructOp: serial 7 (unchanged)
- Installed StructOp/c v2.3.0
- Installed StructOp/rust v2.3.0

StructOp has been successfully initialized!
$ ls -A .structop
providers
providers.lock
Enter fullscreen mode Exit fullscreen mode

init installs the providers the project names into .structop/providers/ and locks their exact versions in .structop/providers.lock. It is all or none: if one provider is refused, nothing is installed and no lock is written. Run it once per project, and again after changing the provider list.

Validate: checked before anything is built

$ structop validate
Success! The configuration is valid.
Enter fullscreen mode Exit fullscreen mode

validate checks the syntax, the types, the names and the references, and each ability's attributes against its provider. It writes nothing.

To see what it catches, we made three mistakes, one at a time, and undid each one.

Three mistakes and the errors validate reports for them

A misspelt name in the loop's body (body = [ability.call.Greeet]):

$ structop validate
Error: unknown ability "ability.call.Greeet"
  on main.st line 11, column 19
  Did you mean "ability.call.Greet"?
Enter fullscreen mode Exit fullscreen mode

Ada's age given as text (age = "thirty-six"):

$ structop validate
Error: field "age" must be an Int: a whole number
  on vars.st line 6, column 31
Enter fullscreen mode Exit fullscreen mode

And a dependency cycle: the newcomer made to depend on the loop (depends_on = [ability.for.Each]), which already depends on the newcomer:

$ structop validate
Error: dependency cycle involving ability.add.Newcomer and ability.for.Each
  on main.st line 1, column 1
Enter fullscreen mode Exit fullscreen mode

Each error names the file, line and column, and each mistake is caught before any code exists.

Plan: see the change before you build it

plan compares the declarations with the state recorded by the last apply, and shows what apply would do. Before the first apply there is no state, so everything is to be created:

$ structop plan
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.add.Newcomer
      + ability.for.Each
      + ability.call.Greet
      + ability.function.greet
      + ability.switch.Mood
      + ability.print.Happy
      + ability.print.Sleepy
      + ability.enum.Mood
      + ability.struct.Guest
      + ability.value.guests
      + ability.value.i

Plan: 1 to add, 0 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

One resource, program.app, with eleven abilities. Plan writes nothing, unless you save it with -out=FILE.

Apply: build, record, run

apply prints the plan, generates the source, and compiles it with the target's own compiler. It records the new state, then runs the program:

$ structop apply
  ...
Plan: 1 to add, 0 to change, 0 to destroy.

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Welcome, Ada! (36)
Shh, Linus is asleep.
Welcome, Grace! (45)
Enter fullscreen mode Exit fullscreen mode

What .structop holds

$ ls -A .structop
build
providers
providers.lock
state.json
$ ls .structop/build
app
main.c
$ structop state list
program.app
Enter fullscreen mode Exit fullscreen mode

The .structop folder: providers and their lock from init, state.json and build from apply

  • providers/ and providers.lock: the installed providers and their exact versions, from init.
  • state.json: what the last apply built, keyed by resource address. The next plan compares against it.
  • build/: the generated source (main.c, or main.rs for Rust) and the compiled program, app.

structop show prints the recorded state, and structop state list lists the recorded resources.

No changes, nothing to do

Run plan again and it has nothing to report. Touching the files changes their times, not their text, so it is not a change either:

$ structop plan
No changes. Your application matches the configuration.
$ touch intent.st types.st vars.st main.st
$ structop plan -detailed-exitcode
No changes. Your application matches the configuration.
Enter fullscreen mode Exit fullscreen mode

plan -detailed-exitcode exited with status 0.

Visualise: graph

structop graph draws the application in the terminal as a flowchart: a box for each top-level ability in run order, dotted lines for what depends on what, and source and reads lines for which data block a value feeds and who reads it. Here it is, redrawn from the terminal:

structop graph, redrawn: the guest list's run order, dependencies and data flow

It shows the declared order and dependencies, not everything the program does when it runs. -format=mermaid gives the same graph to other tools.

One change, planned

Now a real change, the one the handbook's lifecycle page uses: one print that counts the guests, after the loop.

ability "print" "Count" {
    message    = "${length(data.value.guests.current)} guests are in the treehouse."
    depends_on = [ability.for.Each]
}
Enter fullscreen mode Exit fullscreen mode

A text diff would show five new lines. The plan shows what they mean:

$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      + ability.print.Count
      ~ ability.enum.Mood
          (run order): 8 -> 9
      ~ ability.struct.Guest
          (run order): 9 -> 10
      ~ ability.value.guests
          (run order): 10 -> 11
      ~ ability.value.i
          (run order): 11 -> 12

Plan: 0 to add, 1 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

The program will be replaced (rebuilt), one ability is added, and four declarations move one place in the run order. Nothing else about them changed.

The added ability and the plan that explains it

structop graph -changed draws the same flowchart with the new ability marked + and the moved ones >. Applying rebuilds, records and runs, and the next plan is empty again:

$ structop apply
  ...
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Welcome, Ada! (36)
Shh, Linus is asleep.
Welcome, Grace! (45)
3 guests are in the treehouse.
$ structop plan
No changes. Your application matches the configuration.
Enter fullscreen mode Exit fullscreen mode

Add, change, destroy

A plan uses three symbols: + to add, ~ to change and - to destroy. The resources it counts are program.app and one export.NAME for each export block. Adding export "dist" { path = "dist/guests" } to intent.st:

$ structop plan
  # export.dist will be created
  + export.dist (dist/guests)

Plan: 1 to add, 0 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

After applying that and removing the block again:

$ structop plan
  # export.dist will be destroyed
  - export.dist (dist/guests)

Plan: 0 to add, 0 to change, 1 to destroy.
Enter fullscreen mode Exit fullscreen mode

Guarantees you can rely on

The handbook lists nine lifecycle guarantees, and StructOp's own automated tests check each one on every change, on macOS and Linux, for C and Rust targets.

The nine lifecycle guarantees, and a missing program and a stale saved plan in the terminal

Two of them on our project. Delete the program, and the next plan says so; apply restores it:

$ rm .structop/build/app
$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      .structop/build/app is missing or was changed outside structop

Plan: 0 to add, 1 to change, 0 to destroy.
$ structop apply -no-run
  ...
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode

Save a plan, edit the files again, and apply refuses the stale plan instead of guessing:

$ structop plan -out=change.plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      ~ ability.print.Sleepy
          message: "Shh, ${guest.name} is asleep." -> "Hush, ${guest.name} is asleep."

Plan: 0 to add, 1 to change, 0 to destroy.

Saved the plan to: change.plan

To perform exactly these actions, run:
    structop apply "change.plan"
$ structop apply change.plan
Error: saved plan is stale: the configuration changed; run structop plan again
Enter fullscreen mode Exit fullscreen mode

And a failed native build changes nothing that is recorded: the state and the program stay as they were, and the next plan still shows the pending change.

Next

That's the lifecycle: validate catches mistakes, plan shows the change, graph draws it, apply builds it, and state remembers. Next episode: inputs, modules and outputs, following the handbook's Inputs and values and Modules and outputs.

Top comments (0)