Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion console/Cargo.lock

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

4 changes: 3 additions & 1 deletion console/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flipper_console"
version = "0.1.1"
version = "0.1.2"
authors = ["Nick Mosher <nicholastmosher@gmail.com>"]
links = "flipper"

Expand All @@ -20,6 +20,8 @@ failure = { git = "https://github.com/withoutboats/failure.git" }
derive-fail = { git = "https://github.com/withoutboats/derive-fail.git" }
fallible-iterator = "*"
flipper = { path = "../languages/rust" }
log = "0.4.1"
env_logger = "0.5.6"

[lib]
name = "flipper_console"
Expand Down
97 changes: 40 additions & 57 deletions console/src/bin/bindings_cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::fs;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
#[allow(unused_imports)] use std::io::Read;
use std::io::Error as IoError;
use clap::{App, AppSettings, Arg, ArgMatches};
use failure::Error;
use flipper_console::CliError;
use console::CliError;
use console::bindings;

#[derive(Debug, Fail)]
#[fail(display = "Errors that occur while generating bindings")]
Expand All @@ -16,73 +19,53 @@ enum BindingError {
}

pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
App::new("binding")
App::new("generate")
.settings(&[
AppSettings::AllowExternalSubcommands,
AppSettings::ArgRequiredElseHelp,
AppSettings::DeriveDisplayOrder,
AppSettings::ColoredHelp,
])
.template(
"{bin} \n\
USAGE: \n {usage} \n\n\
{all-args}"
)
.subcommands(vec![
generate::make_subcommand(),
])
.alias("gen")
.about("Generate Flipper language bindings")
.before_help("Generate bindings for the specified module")
.arg(Arg::with_name("file")
.index(1)
.takes_value(true)
.required(true)
.help("The module file to generate language bindings for"))
.arg(Arg::with_name("output")
.long("--output")
.short("-o")
.takes_value(true)
.help("The output directory for the bindings"))
}

pub fn execute(args: &ArgMatches) -> Result<(), Error> {
match args.subcommand() {
("generate", Some(m)) => generate::execute(m),
(unknown, _) => Err(CliError::UnrecognizedCommand(unknown.to_owned()).into()),
}
}
// Guaranteed to be safe because these are required arguments.
let filename = args.value_of("file").unwrap();

/// Usage: `flipper generate [LANG]`, where `[LANG]` is one of:
/// --java, --javascript, --python, --objc, --swift, --rust
/// Flipper modules can be executed remotely from a host machine
/// such as a laptop or mobile phone.
pub mod generate {
use super::*;
use flipper_console::bindings;
let mut file = File::open(filename)
.map_err(|e| BindingError::FileError(filename.to_owned(), e))?;

pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
App::new("generate")
.alias("gen")
.about("Generate Flipper language bindings")
.before_help("Generate bindings for the specified module")
.arg(Arg::with_name("file")
.index(1)
.takes_value(true)
.required(true)
.help("The module file to generate language bindings for"))
.arg(Arg::with_name("name")
.index(2)
.takes_value(true)
.required(true)
.help("The name of the module"))
}
let path = PathBuf::from(args.value_of("output").unwrap_or("./"));
fs::create_dir_all(&path);

pub fn execute(args: &ArgMatches) -> Result<(), Error> {
// Guaranteed to be safe because these are required arguments.
let filename = args.value_of("file").unwrap();
let module_name = args.value_of("name").unwrap();
let module_binary = {
let mut v = Vec::new();
let _ = file.read_to_end(&mut v);
v
};

let mut file = File::open(filename)
.map_err(|e| BindingError::FileError(filename.to_owned(), e))?;
let modules = bindings::Module::parse(&module_binary)?;
for module in modules.iter() {
let mut module_name = module.name.clone();
module_name.push_str(".c");
let mut path = PathBuf::from(&path);
path.push(&module_name);

let module_binary = {
let mut v = Vec::new();
let _ = file.read_to_end(&mut v);
v
};

let mut out = File::create("./binding.c")
.map_err(|e| BindingError::FileError("binding.c".to_owned(), e))?;

let module = bindings::Module::parse(String::from(module_name), "".to_owned(), &module_binary)?;
bindings::generators::c::generate_module(module, &mut out)
let mut out = File::create(&path)?;
bindings::generators::c::generate_module(module, &mut out)?;
}

Ok(())
}
Loading