|
1 | 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | 2 |
|
3 | | -pub mod combined; |
4 | 3 | pub mod intercept; |
5 | 4 | pub mod semantic; |
6 | 5 |
|
| 6 | +use crate::modes::intercept::Interceptor; |
| 7 | +use crate::modes::semantic::SemanticAnalysisPipeline; |
| 8 | +use crate::output::event::{read, write}; |
| 9 | +use crate::{args, config}; |
| 10 | +use anyhow::Context; |
| 11 | +use std::fs::{File, OpenOptions}; |
| 12 | +use std::io; |
| 13 | +use std::io::BufReader; |
7 | 14 | use std::process::ExitCode; |
8 | 15 |
|
9 | 16 | /// The mode trait is used to run the application in different modes. |
10 | 17 | pub trait Mode { |
11 | 18 | fn run(self) -> anyhow::Result<ExitCode>; |
12 | 19 | } |
| 20 | + |
| 21 | +/// The intercept mode we are only capturing the build commands |
| 22 | +/// and write it into the output file. |
| 23 | +pub struct Intercept { |
| 24 | + command: args::BuildCommand, |
| 25 | + interceptor: Interceptor, |
| 26 | +} |
| 27 | + |
| 28 | +impl Intercept { |
| 29 | + /// Create a new intercept mode instance. |
| 30 | + pub fn from( |
| 31 | + command: args::BuildCommand, |
| 32 | + output: args::BuildEvents, |
| 33 | + config: config::Main, |
| 34 | + ) -> anyhow::Result<Self> { |
| 35 | + let file_name = output.file_name.as_str(); |
| 36 | + let output_file = OpenOptions::new() |
| 37 | + .write(true) |
| 38 | + .create(true) |
| 39 | + .truncate(true) |
| 40 | + .open(file_name) |
| 41 | + .map(io::BufWriter::new) |
| 42 | + .with_context(|| format!("Failed to open file: {:?}", file_name))?; |
| 43 | + |
| 44 | + let interceptor: Interceptor = |
| 45 | + Interceptor::new(config, move |envelopes| write(output_file, envelopes))?; |
| 46 | + |
| 47 | + Ok(Self { |
| 48 | + command, |
| 49 | + interceptor, |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl Mode for Intercept { |
| 55 | + /// Run the intercept mode by setting up the collector service and |
| 56 | + /// the intercept environment. The build command is executed in the |
| 57 | + /// intercept environment. |
| 58 | + /// |
| 59 | + /// The exit code is based on the result of the build command. |
| 60 | + fn run(self) -> anyhow::Result<ExitCode> { |
| 61 | + self.interceptor.run_build_command(self.command) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// The semantic mode we are deduct the semantic meaning of the |
| 66 | +/// executed commands from the build process. |
| 67 | +pub struct Semantic { |
| 68 | + event_file: BufReader<File>, |
| 69 | + semantic: SemanticAnalysisPipeline, |
| 70 | +} |
| 71 | + |
| 72 | +impl Semantic { |
| 73 | + pub fn from( |
| 74 | + input: args::BuildEvents, |
| 75 | + output: args::BuildSemantic, |
| 76 | + config: config::Main, |
| 77 | + ) -> anyhow::Result<Self> { |
| 78 | + let file_name = input.file_name.as_str(); |
| 79 | + let event_file = OpenOptions::new() |
| 80 | + .read(true) |
| 81 | + .open(file_name) |
| 82 | + .map(BufReader::new) |
| 83 | + .with_context(|| format!("Failed to open file: {:?}", file_name))?; |
| 84 | + |
| 85 | + let semantic = SemanticAnalysisPipeline::from(output, &config)?; |
| 86 | + |
| 87 | + Ok(Self { |
| 88 | + event_file, |
| 89 | + semantic, |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Mode for Semantic { |
| 95 | + /// Run the semantic mode by reading the event file and analyzing the events. |
| 96 | + /// |
| 97 | + /// The exit code is based on the result of the output writer. |
| 98 | + fn run(self) -> anyhow::Result<ExitCode> { |
| 99 | + self.semantic |
| 100 | + .analyze_and_write(read(self.event_file)) |
| 101 | + .map(|_| ExitCode::SUCCESS) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// The all model is combining the intercept and semantic modes. |
| 106 | +pub struct Combined { |
| 107 | + command: args::BuildCommand, |
| 108 | + interceptor: Interceptor, |
| 109 | +} |
| 110 | + |
| 111 | +impl Combined { |
| 112 | + /// Create a new all mode instance. |
| 113 | + pub fn from( |
| 114 | + command: args::BuildCommand, |
| 115 | + output: args::BuildSemantic, |
| 116 | + config: config::Main, |
| 117 | + ) -> anyhow::Result<Self> { |
| 118 | + let semantic = SemanticAnalysisPipeline::from(output, &config)?; |
| 119 | + let interceptor: Interceptor = Interceptor::new(config, move |envelopes| { |
| 120 | + semantic.analyze_and_write(envelopes) |
| 121 | + })?; |
| 122 | + |
| 123 | + Ok(Self { |
| 124 | + command, |
| 125 | + interceptor, |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl Mode for Combined { |
| 131 | + /// Run the all mode by setting up the collector service and the intercept environment. |
| 132 | + /// The build command is executed in the intercept environment. The collected events are |
| 133 | + /// then processed by the semantic recognition and transformation. The result is written |
| 134 | + /// to the output file. |
| 135 | + /// |
| 136 | + /// The exit code is based on the result of the build command. |
| 137 | + fn run(self) -> anyhow::Result<ExitCode> { |
| 138 | + self.interceptor.run_build_command(self.command) |
| 139 | + } |
| 140 | +} |
0 commit comments