Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL and serde support #1154

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
396 changes: 327 additions & 69 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,24 @@ path = "src/makers.rs"

[dependencies]
cargo_metadata = "^0.18"
ci_info = "^0.14.14"
ci_info = { version = "^0.14.14", features = ["serde-1"] }
cliparser = "^0.1.2"
colored = "^2"
ctrlc = "^3"
diesel = { version = "^2", optional = true, features = [
"serde_json",
"r2d2",
"postgres",
"64-column-tables",
] }
diesel_migrations = { version = "^2", optional = true }
dirs-next = "^2"
duckscript = "^0.8.0"
duckscriptsdk = { version = "^0.9.3", default-features = false }
envmnt = "^0.10.4"
fern = "^0.6"
fsio = { version = "^0.4", features = ["temp-path"] }
git_info = "^0.1.2"
git_info = { version = "0.1.3", features = ["serde"] }
glob = "^0.3.1"
home = "^0.5"
ignore = "^0.4"
Expand All @@ -67,7 +74,7 @@ once_cell = "^1.19.0"
petgraph = "^0.6.5"
regex = "^1.10"
run_script = "^0.10"
rust_info = "^0.3.1"
rust_info = { version = "0.3.3", features = ["serde"] }
semver = "^1"
serde = "^1"
serde_derive = "^1"
Expand All @@ -87,7 +94,9 @@ expect-test = "^1"
nu-ansi-term = "^0.50"

[features]
diesel = ["dep:diesel"]
diesel_migrations = ["dep:diesel_migrations"]
tls-rustls = ["duckscriptsdk/tls-rustls"]
tls-native = ["duckscriptsdk/tls-native"]
tls = ["tls-rustls"] # alias for backward compatibility
tls = ["tls-rustls"] # alias for backward compatibility
default = ["tls-rustls"]
24 changes: 18 additions & 6 deletions src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::recursion_level;
use crate::runner;
use crate::time_summary;
use crate::toolchain;
use crate::types::{CliArgs, GlobalConfig};
use crate::types::{CliArgs, Config, EnvInfo, GlobalConfig};
use crate::version;
use std::time::SystemTime;

Expand All @@ -32,11 +32,11 @@ pub(crate) static DEFAULT_LOG_LEVEL: &str = "info";
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";

pub fn run(
cli_args: &CliArgs,
pub fn prepare_for_execution_plan<'a>(
cli_args: &'a CliArgs,
global_config: &GlobalConfig,
logger_options: Option<LoggerOptions>,
) -> Result<(), CargoMakeError> {
) -> Result<(EnvInfo, Config, &'a String, Vec<(String, u128)>, SystemTime), CargoMakeError> {
let start_time = SystemTime::now();

recursion_level::increment();
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn run(
let experimental = cli_args.experimental;
let config = descriptor::load(&build_file, force_makefile, env, experimental)?;

let mut time_summary_vec = vec![];
let mut time_summary_vec = Vec::<(String, u128)>::new();
time_summary::add(
&mut time_summary_vec,
"[Load Makefiles]",
Expand All @@ -130,6 +130,17 @@ pub fn run(
// ensure profile env was not overridden
profile::set(&normalized_profile_name);

Ok((env_info, config, task, time_summary_vec, start_time))
}

pub fn run(
cli_args: &CliArgs,
global_config: &GlobalConfig,
logger_options: Option<LoggerOptions>,
) -> Result<(), CargoMakeError> {
let (env_info, config, task, time_summary_vec, start_time) =
prepare_for_execution_plan(cli_args, global_config, logger_options)?;

if cli_args.list_all_steps || cli_args.list_category_steps.is_some() {
cli_commands::list_steps::run(
&config,
Expand All @@ -139,7 +150,8 @@ pub fn run(
cli_args.hide_uninteresting,
)
} else if cli_args.diff_execution_plan {
let default_config = descriptor::load_internal_descriptors(true, experimental, None)?;
let default_config =
descriptor::load_internal_descriptors(true, cli_args.experimental, None)?;
cli_commands::diff_steps::run(
&default_config,
&config,
Expand Down
10 changes: 7 additions & 3 deletions src/lib/cli_commands/diff_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ use crate::command;
use crate::error::CargoMakeError;
use crate::execution_plan::ExecutionPlanBuilder;
use crate::io::{create_file, delete_file};
use crate::types::{CliArgs, Config, CrateInfo, ExecutionPlan};
use crate::types::{CliArgs, Config, CrateInfo, ExecutionPlan, SerdeRegex};
use regex::Regex;
use std::fs::File;
use std::io;
use std::io::{BufWriter, Write};

fn write_as_string(execution_plan: &ExecutionPlan, file: &File) -> io::Result<()> {
let mut writer = BufWriter::new(file);
writeln!(&mut writer, "{:#?}", &execution_plan.steps)
writeln!(
&mut writer,
"{:#?}",
&execution_plan.steps[execution_plan.steps_to_run.clone()]
)
}

/// Runs the execution plan diff
Expand All @@ -32,7 +36,7 @@ pub(crate) fn run(
) -> Result<(), CargoMakeError> {
let skip_tasks_pattern = match cli_args.skip_tasks_pattern {
Some(ref pattern) => match Regex::new(pattern) {
Ok(reg) => Some(reg),
Ok(reg) => Some(SerdeRegex(reg)),
Err(_) => {
warn!("Invalid skip tasks pattern provided: {}", pattern);
None
Expand Down
23 changes: 18 additions & 5 deletions src/lib/cli_commands/print_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::CargoMakeError;
use std::io;

use crate::execution_plan::ExecutionPlanBuilder;
use crate::types::{Config, CrateInfo, ExecutionPlan};
use crate::types::{Config, CrateInfo, ExecutionPlan, SerdeRegex};
use regex::Regex;

#[derive(Debug)]
Expand Down Expand Up @@ -50,7 +50,7 @@ fn print_short_description(
execution_plan: &ExecutionPlan,
) -> io::Result<()> {
let mut counter = 1;
for step in &execution_plan.steps {
for step in &execution_plan.steps[execution_plan.steps_to_run.clone()] {
let task = &step.config;
let description = match &task.description {
Some(value) => value,
Expand All @@ -62,7 +62,7 @@ fn print_short_description(
counter, &step.name, &description
)?;

counter = counter + 1;
counter += 1;
}
Ok(())
}
Expand All @@ -71,7 +71,20 @@ fn print_default(
output_buffer: &mut impl io::Write,
execution_plan: &ExecutionPlan,
) -> io::Result<()> {
writeln!(output_buffer, "{:#?}", &execution_plan)
let steps: Vec<crate::types::Step> = {
let mut _steps =
Vec::<crate::types::Step>::with_capacity(execution_plan.steps_to_run.len());
execution_plan.steps[execution_plan.steps_to_run.clone()].clone_into(&mut _steps);
_steps
};
writeln!(
output_buffer,
"{:#?}",
ExecutionPlan {
steps,
..ExecutionPlan::default()
}
)
}

/// Only prints the execution plan
Expand All @@ -87,7 +100,7 @@ pub fn print(
) -> Result<(), CargoMakeError> {
let skip_tasks_pattern_regex = match skip_tasks_pattern {
Some(ref pattern) => match Regex::new(pattern) {
Ok(reg) => Some(reg),
Ok(reg) => Some(SerdeRegex(reg)),
Err(_) => {
warn!("Invalid skip tasks pattern provided: {}", pattern);
None
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cli_commands/print_steps_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn print_default_valid() {
config: Task::new(),
};
let steps = vec![step];
let execution_plan = ExecutionPlan { steps };
let execution_plan = ExecutionPlan::new(steps);

print_default(&mut std::io::stdout(), &execution_plan).expect("print should succeed");
}
Expand All @@ -122,7 +122,7 @@ fn print_short_description_valid() {
config: Task::new(),
};
let steps = vec![step];
let execution_plan = ExecutionPlan { steps };
let execution_plan = ExecutionPlan::new(steps);

print_short_description(&mut std::io::stdout(), &execution_plan).expect("print should succeed");
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/diesel_specific/diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]

[migrations_directory]
dir = "src/lib/diesel_specific/migrations"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE tasks;

DROP TABLE steps;

DROP TABLE execution_plans;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
CREATE TABLE tasks
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
clear BOOLEAN,
description TEXT,
category TEXT,
disabled BOOLEAN,
private BOOLEAN,
deprecated JSONB,
extend TEXT,
workspace BOOLEAN,
plugin TEXT,
watch JSONB,
condition JSONB,
condition_script JSONB,
condition_script_runner_args JSONB,
ignore_errors BOOLEAN,
force BOOLEAN,
env_files JSONB,
env JSONB,
cwd TEXT,
alias TEXT,
linux_alias TEXT,
windows_alias TEXT,
mac_alias TEXT,
install_crate JSONB,
install_crate_args JSONB,
install_script JSONB,
command TEXT,
args JSONB,
script JSONB,
script_runner TEXT,
script_runner_args JSONB,
script_extension TEXT,
run_task JSONB,
dependencies JSONB,
toolchain JSONB,
linux JSONB,
windows JSONB,
mac JSONB
);

CREATE TABLE steps
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name TEXT NOT NULL,
config JSONB NOT NULL
);

CREATE TABLE execution_plans
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
steps JSONB NOT NULL,
steps_to_run JSONB NOT NULL,
name TEXT NOT NULL
);
3 changes: 3 additions & 0 deletions src/lib/diesel_specific/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#[path = "src/schema.rs"]
pub mod schema;
pub use crate::diesel_specific;
63 changes: 63 additions & 0 deletions src/lib/diesel_specific/src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @generated automatically by Diesel CLI.

diesel::table! {
execution_plans (id) {
id -> Int4,
steps -> Jsonb,
steps_to_run -> Jsonb,
name -> Text,
}
}

diesel::table! {
steps (id) {
id -> Int4,
name -> Text,
config -> Jsonb,
}
}

diesel::table! {
tasks (id) {
id -> Int4,
clear -> Nullable<Bool>,
description -> Nullable<Text>,
category -> Nullable<Text>,
disabled -> Nullable<Bool>,
private -> Nullable<Bool>,
deprecated -> Nullable<Jsonb>,
extend -> Nullable<Text>,
workspace -> Nullable<Bool>,
plugin -> Nullable<Text>,
watch -> Nullable<Jsonb>,
condition -> Nullable<Jsonb>,
condition_script -> Nullable<Jsonb>,
condition_script_runner_args -> Nullable<Jsonb>,
ignore_errors -> Nullable<Bool>,
force -> Nullable<Bool>,
env_files -> Nullable<Jsonb>,
env -> Nullable<Jsonb>,
cwd -> Nullable<Text>,
alias -> Nullable<Text>,
linux_alias -> Nullable<Text>,
windows_alias -> Nullable<Text>,
mac_alias -> Nullable<Text>,
install_crate -> Nullable<Jsonb>,
install_crate_args -> Nullable<Jsonb>,
install_script -> Nullable<Jsonb>,
command -> Nullable<Text>,
args -> Nullable<Jsonb>,
script -> Nullable<Jsonb>,
script_runner -> Nullable<Text>,
script_runner_args -> Nullable<Jsonb>,
script_extension -> Nullable<Text>,
run_task -> Nullable<Jsonb>,
dependencies -> Nullable<Jsonb>,
toolchain -> Nullable<Jsonb>,
linux -> Nullable<Jsonb>,
windows -> Nullable<Jsonb>,
mac -> Nullable<Jsonb>,
}
}

diesel::allow_tables_to_appear_in_same_query!(execution_plans, steps, tasks,);
Loading