Skip to content

Commit

Permalink
feat: make output projects
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 2, 2021
1 parent 1930cd2 commit 891d1da
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions _fixtures/pipeline/jenkinsfile/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pipeline {
agent none
stages {
stage('Sequential') {
agent {
label 'for-sequential'
}
environment {
FOR_SEQUENTIAL = "some-value"
}
stages {
stage('In Sequential 1') {
steps {
echo "In Sequential 1"
}
}
stage('In Sequential 2') {
steps {
echo "In Sequential 2"
}
}
}
}
}
}
4 changes: 4 additions & 0 deletions core_model/src/coco_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl Settings {
Settings::reporter(Some("framework"))
}

pub fn pipeline() -> PathBuf {
Settings::reporter(Some("pipeline"))
}

pub fn struct_dir() -> PathBuf {
Settings::reporter(Some("struct"))
}
Expand Down
6 changes: 5 additions & 1 deletion plugins/coco_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jenkinsfile = "0.2.0"
jenkinsfile = "0.2.1"

# grammar generator
pest = "2.1.3"
Expand All @@ -21,6 +21,10 @@ serde_json = "1"
# Option parser with custom derive support
gumdrop = "0.8"

# gitignore
# docs: https://github.com/BurntSushi/ripgrep/tree/master/crates/ignore
ignore = "0.4"

[dependencies.core_model]
path = "../../core_model"

Expand Down
46 changes: 40 additions & 6 deletions plugins/coco_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// extern crate pest;
// #[macro_use]
// extern crate pest_derive;

pub mod github_action;
pub mod jenkinsfile;
pub mod pipeline;
pub mod pipeline_analysis;
pub mod pipeline_plugin;

use core_model::CocoConfig;
use plugin_interface::PluginInterface;
Expand All @@ -22,7 +18,7 @@ impl PluginInterface for CocoPipeline {
fn on_plugin_unload(&self) {}

fn execute(&self, config: CocoConfig) {
pipeline_analysis::execute(config);
pipeline_plugin::execute(config);
}
}

Expand All @@ -36,3 +32,41 @@ impl Default for CocoPipeline {
pub fn plugin() -> Box<dyn PluginInterface> {
Box::new(CocoPipeline::default())
}

#[cfg(test)]
mod tests {
use crate::pipeline_plugin::execute;
use core_model::{CocoConfig, RepoConfig};
use std::path::PathBuf;

pub fn ctags_fixtures_dir() -> PathBuf {
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf();
let ctags_dir = root_dir
.clone()
.join("_fixtures")
.join("pipeline")
.join("jenkinsfile");

return ctags_dir;
}

#[test]
fn should_run_pipeline_analysis() {
let mut repos = vec![];
repos.push(RepoConfig {
url: format!("{}", ctags_fixtures_dir().display()),
languages: None,
});
let config = CocoConfig {
repos: repos,
plugins: None,
};

execute(config);
}
}
5 changes: 0 additions & 5 deletions plugins/coco_pipeline/src/pipeline_analysis.rs

This file was deleted.

48 changes: 48 additions & 0 deletions plugins/coco_pipeline/src/pipeline_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use core_model::url_format::uri_to_path;
use core_model::{url_format, CocoConfig, Settings};
use ignore::Walk;
use jenkinsfile::Jenkinsfile;
use std::fs;
use std::path::PathBuf;

pub fn execute(config: CocoConfig) {
for repo in &config.repos {
let url_str = repo.url.as_str();
let origin_files = lookup_jenkinsfile(url_str);
let mut results = vec![];

for path in origin_files {
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
if let Some(jenkinsfile) = Jenkinsfile::from_str(contents.as_str()) {
results.push(jenkinsfile);
}
}

let result = serde_json::to_string_pretty(&results).unwrap();
write_to_json_file(url_str, &result);
}
}

fn write_to_json_file(url_str: &str, result: &String) {
let file_name = url_format::json_filename(url_str);
let output_file = Settings::pipeline().join(file_name);
fs::write(output_file, result).expect("cannot write file");
}

fn lookup_jenkinsfile(url_str: &str) -> Vec<PathBuf> {
let path = uri_to_path(url_str);
let mut origin_files = vec![];
for result in Walk::new(path) {
if let Ok(entry) = result {
if !entry.file_type().unwrap().is_file() {
continue;
}

if entry.file_name().to_str().unwrap() == "Jenkinsfile" {
origin_files.push(entry.into_path());
}
}
}

origin_files
}

0 comments on commit 891d1da

Please sign in to comment.