Implement the parser support for the parallel block

This was pretty simple, yay.

pipeline {
    parallel {
        stage {
            name = 'Foo'
            steps {
                sh 'pwd'
            }
        }
        stage {
            name = 'Foo'
            steps {
                sh 'pwd'
            }
        }
    }
}
This commit is contained in:
R Tyler Croy 2020-11-28 22:03:34 -08:00
parent 127d3b7aa4
commit 0b4821137d
3 changed files with 57 additions and 4 deletions

1
crates/parser/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

View File

@ -47,6 +47,12 @@ pub fn parse_pipeline_string(buffer: &str) -> Result<Pipeline, PestError<Rule>>
contexts: vec![ctx],
});
}
Rule::parallel => {
pipeline.batches.push(Batch {
mode: BatchMode::Parallel,
contexts: parse_parallel(&mut parsed.into_inner()),
});
}
_ => {}
}
}
@ -58,7 +64,21 @@ pub fn parse_pipeline_string(buffer: &str) -> Result<Pipeline, PestError<Rule>>
Ok(pipeline)
}
fn parse_str(parser: &mut pest::iterators::Pair<Rule>) -> String {
fn parse_parallel(parser: &mut Pairs<Rule>) -> Vec<Context> {
let mut contexts = vec![];
while let Some(parsed) = parser.next() {
match parsed.as_rule() {
Rule::stage => {
let ctx = parse_stage(&mut parsed.into_inner());
contexts.push(ctx);
}
_ => {}
}
}
contexts
}
fn parse_str(parser: &mut Pair<Rule>) -> String {
// TODO: There's got to be a better way than cloning
let mut parser = parser.clone().into_inner();
while let Some(parsed) = parser.next() {
@ -394,4 +414,27 @@ mod tests {
}
}
}
#[test]
fn parse_parallel() {
let buf = r#"
pipeline {
parallel {
stage {
name = 'Test'
steps { sh 'ls' }
}
stage {
name = 'UAT'
steps { sh 'pwd' }
}
}
}"#;
let pipeline = parse_pipeline_string(&buf).expect("Failed to parse");
assert!(!pipeline.uuid.is_nil());
assert_eq!(pipeline.batches.len(), 1);
let batch = &pipeline.batches[0];
assert_eq!(batch.contexts.len(), 2);
}
}

View File

@ -1,18 +1,27 @@
// The pipeline PEG
// The pipeline PEG
pipeline = _{ SOI ~ "pipeline" ~
BLOCK_BEGIN ~
execBlocks ~
BLOCK_END ~ EOI }
execBlocks = { (stage | steps)* }
execBlocks = { (stage
| steps
| parallel)* }
stage = { "stage" ~
BLOCK_BEGIN ~
BLOCK_BEGIN ~
(property*) ~
steps ~
BLOCK_END }
// The parallel block can contain multiple stages which run in parallel
//
// inside the parser this should result in multiple contexts in the same batch
parallel = { "parallel" ~
BLOCK_BEGIN ~
(stage)* ~
BLOCK_END }
steps = { "steps" ~ BLOCK_BEGIN ~ step+ ~ BLOCK_END }
step = { IDENT ~ (