-
Notifications
You must be signed in to change notification settings - Fork 1
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
Librarify #50
Draft
petervdonovan
wants to merge
8
commits into
main
Choose a base branch
from
librarify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Librarify #50
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2c2a9c1
Begin to support use of lingo as a library
petervdonovan 7e24f79
Make LfcJsonArgs public.
petervdonovan f77354a
Isolate deps that can't wasm
petervdonovan cec460d
Capabilitify read_to_string
petervdonovan 92ec3ab
Rename lingua-franca -> lingo for package name
petervdonovan cbb0b2e
Use lifetimes to improve restriction on capability
petervdonovan f5153d3
Make logging externally configurable
petervdonovan 3ebb401
Minor updates, including making CCpp a target
petervdonovan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ use std::path::PathBuf; | |
#[clap(rename_all = "lowercase")] | ||
pub enum TargetLanguage { | ||
C, | ||
CCpp, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
Cpp, | ||
Rust, | ||
TypeScript, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::io; | ||
|
||
pub mod args; | ||
pub mod backends; | ||
pub mod package; | ||
pub mod util; | ||
|
||
#[derive(Debug)] | ||
pub enum WhichError { | ||
/// An executable binary with that name was not found | ||
CannotFindBinaryPath, | ||
/// There was nowhere to search and the provided name wasn't an absolute path | ||
CannotGetCurrentDirAndPathListEmpty, | ||
/// Failed to canonicalize the path found | ||
CannotCanonicalize, | ||
} | ||
#[derive(Debug)] | ||
pub struct GitCloneError(pub String); // TODO: create a more domain-specific error time like the actual git2::Error | ||
|
||
impl std::fmt::Display for WhichError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
WhichError::CannotFindBinaryPath => write!(f, "cannot find binary"), | ||
WhichError::CannotGetCurrentDirAndPathListEmpty => { | ||
write!(f, "cannot get current dir and path list empty") | ||
} | ||
WhichError::CannotCanonicalize => write!(f, "cannot canonicalize"), | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for GitCloneError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl std::error::Error for WhichError {} | ||
|
||
impl std::error::Error for GitCloneError {} | ||
|
||
pub struct GitUrl<'a>(&'a str); | ||
|
||
impl<'a> From<&'a str> for GitUrl<'a> { | ||
fn from(value: &'a str) -> Self { | ||
GitUrl(value) | ||
} | ||
} | ||
|
||
impl<'a> From<GitUrl<'a>> for &'a str { | ||
fn from(value: GitUrl<'a>) -> Self { | ||
value.0 | ||
} | ||
} | ||
|
||
pub type WhichCapability<'a> = Box<dyn Fn(&str) -> Result<std::path::PathBuf, WhichError> + 'a>; | ||
pub type GitCloneCapability<'a> = | ||
Box<dyn Fn(GitUrl, &std::path::Path) -> Result<(), GitCloneError> + 'a>; | ||
pub type FsReadCapability<'a> = Box<dyn Fn(&std::path::Path) -> io::Result<String> + 'a>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
use std::io::ErrorKind; | ||
use std::path::Path; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
use std::{env, io}; | ||
|
||
use clap::Parser; | ||
|
||
use crate::args::InitArgs; | ||
use args::{BuildArgs, Command as ConsoleCommand, CommandLineArgs}; | ||
use package::App; | ||
use git2::Repository; | ||
use liblingo::args::InitArgs; | ||
use liblingo::args::{BuildArgs, Command as ConsoleCommand, CommandLineArgs}; | ||
|
||
use crate::backends::{BatchBuildResults, BuildCommandOptions, CommandSpec}; | ||
use crate::package::{Config, ConfigFile}; | ||
use crate::util::errors::{BuildResult, LingoError}; | ||
use liblingo::backends::{BatchBuildResults, BuildCommandOptions, CommandSpec}; | ||
use liblingo::package::{Config, ConfigFile}; | ||
use liblingo::util::errors::{BuildResult, LingoError}; | ||
use liblingo::{GitCloneError, GitUrl, WhichError}; | ||
|
||
pub mod args; | ||
pub mod backends; | ||
pub mod package; | ||
pub(crate) mod util; | ||
fn do_repo_clone(url: GitUrl, into: &std::path::Path) -> Result<(), GitCloneError> { | ||
Repository::clone(url.into(), into).map_or_else( | ||
|err: git2::Error| Err(GitCloneError(format!("{}", err))), | ||
|_| Ok(()), | ||
) | ||
} | ||
|
||
fn do_which(cmd: &str) -> Result<PathBuf, WhichError> { | ||
which::which(cmd).map_err(|err| match err { | ||
which::Error::CannotFindBinaryPath => WhichError::CannotFindBinaryPath, | ||
which::Error::CannotGetCurrentDirAndPathListEmpty => { | ||
WhichError::CannotGetCurrentDirAndPathListEmpty | ||
} | ||
which::Error::CannotCanonicalize => WhichError::CannotCanonicalize, | ||
}) | ||
} | ||
|
||
fn do_read_to_string(p: &Path) -> io::Result<String> { | ||
std::fs::read_to_string(p) | ||
} | ||
|
||
fn main() { | ||
print_logger::new().init().unwrap(); | ||
// parses command line arguments | ||
let args = CommandLineArgs::parse(); | ||
|
||
// Finds Lingo.toml recursively inside the parent directories. | ||
// If it exists the returned path is absolute. | ||
let lingo_path = util::find_toml(&env::current_dir().unwrap()); | ||
let lingo_path = liblingo::util::find_toml(&env::current_dir().unwrap()); | ||
|
||
// tries to read Lingo.toml | ||
let mut wrapped_config = lingo_path.as_ref().and_then(|path| { | ||
ConfigFile::from(path) | ||
.map_err(|err| println!("Error while reading Lingo.toml: {}", err)) | ||
ConfigFile::from(path, Box::new(do_read_to_string)) | ||
.map_err(|err| log::error!("Error while reading Lingo.toml: {}", err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we now want to switch to a proper logging library, we probably need some non-standard schema. |
||
.ok() | ||
.map(|cf| cf.to_config(path.parent().unwrap())) | ||
}); | ||
|
@@ -51,7 +69,7 @@ fn print_res(result: BuildResult) { | |
match result { | ||
Ok(_) => {} | ||
Err(errs) => { | ||
println!("{}", errs); | ||
log::error!("{}", errs); | ||
} | ||
} | ||
} | ||
|
@@ -87,14 +105,14 @@ fn execute_command(config: Option<&Config>, command: ConsoleCommand) -> CommandR | |
"Error: Missing Lingo.toml file", | ||
)))), | ||
(Some(config), ConsoleCommand::Build(build_command_args)) => { | ||
println!("Building ..."); | ||
log::info!("Building ..."); | ||
CommandResult::Batch(build(&build_command_args, config)) | ||
} | ||
(Some(config), ConsoleCommand::Run(build_command_args)) => { | ||
let mut res = build(&build_command_args, config); | ||
res.map(|app| { | ||
let mut command = Command::new(app.executable_path()); | ||
util::run_and_capture(&mut command)?; | ||
liblingo::util::run_and_capture(&mut command)?; | ||
Ok(()) | ||
}); | ||
CommandResult::Batch(res) | ||
|
@@ -109,15 +127,16 @@ fn execute_command(config: Option<&Config>, command: ConsoleCommand) -> CommandR | |
fn do_init(init_config: InitArgs) -> BuildResult { | ||
let initial_config = ConfigFile::new_for_init_task(init_config)?; | ||
initial_config.write(Path::new("./Lingo.toml"))?; | ||
initial_config.setup_example() | ||
initial_config.setup_example(Box::new(do_repo_clone)) | ||
} | ||
|
||
fn build<'a>(args: &BuildArgs, config: &'a Config) -> BatchBuildResults<'a> { | ||
run_command( | ||
CommandSpec::Build(BuildCommandOptions { | ||
profile: args.build_profile(), | ||
compile_target_code: !args.no_compile, | ||
lfc_exec_path: util::find_lfc_exec(args).expect("TODO replace me"), | ||
lfc_exec_path: liblingo::util::find_lfc_exec(args, Box::new(do_which)) | ||
.expect("TODO replace me"), | ||
max_threads: args.threads, | ||
keep_going: args.keep_going, | ||
}), | ||
|
@@ -128,7 +147,7 @@ fn build<'a>(args: &BuildArgs, config: &'a Config) -> BatchBuildResults<'a> { | |
|
||
fn run_command(task: CommandSpec, config: &Config, _fail_at_end: bool) -> BatchBuildResults { | ||
let apps = config.apps.iter().collect::<Vec<_>>(); | ||
backends::execute_command(&task, &apps) | ||
liblingo::backends::execute_command(&task, &apps, Box::new(do_which)) | ||
} | ||
|
||
enum CommandResult<'a> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave it as lingua-franca because we still want to squat that name on crates.io ;D