Skip to content
Merged
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
94 changes: 5 additions & 89 deletions src/bin/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{env, fs, os, panic, path::Path, process, thread, time::Duration};
use std::{env, panic, process, thread, time::Duration};

use ABCoder::{
compress::{
compress::{compress_all, from_json},
conv::convert_go2rust,
types::types::CodeCache,
},
compress::{compress::compress_all, types::types::CodeCache},
config::CONFIG,
repo,
};
Expand All @@ -33,7 +29,6 @@ struct Options {
#[derive(Clone, Debug)]
enum Action {
Compress(CompressAction),
Translate(TranslateAction),
}

#[derive(Clone, Debug, Default)]
Expand All @@ -43,12 +38,6 @@ struct CompressAction {
not_load_external_symbol: bool,
}

#[derive(Clone, Debug)]
enum TranslateAction {
Go2Rust,
Unknown,
}

fn main() {
// parse Options from cmd args
let options = parse_options();
Expand All @@ -64,30 +53,15 @@ fn main() {
export_compress(&options.repo_path, &cmp);
}
}
Action::Translate(trans) => {
match trans {
TranslateAction::Go2Rust => {
// go转rust
trans_go2rust(&options.repo_path);
}
TranslateAction::Unknown => {
println!("{}", USAGE);
process::exit(1);
}
}
}
}
}

const USAGE: &str = "Usage: ABCoder <Action> <RepoPath> [Flags]
Action: compress | translate
Action: compress
compress: compress the repo. Including flags:
--export-compress: export the compress result
--force-update-ast: force parsing repo and merge the previous result
--not-load-external-symbol: not load external external symbols to speed up parsing
translate: translate the repo. Including flags:
--go2rust: translate the repo from go to rust
";
--not-load-external-symbol: not load external external symbols to speed up parsing";

fn parse_options() -> Options {
let args: Vec<String> = env::args().collect();
Expand Down Expand Up @@ -118,20 +92,7 @@ fn parse_options() -> Options {

Action::Compress(compress_action)
}
"translate" => {
let mut translate_action = TranslateAction::Unknown;
if args.len() > 3 {
for i in 3..args.len() {
match args[i].as_str() {
"--go2rust" => {
translate_action = TranslateAction::Go2Rust;
}
_ => {}
}
}
}
Action::Translate(translate_action)
}

_ => {
println!("{}", USAGE);
process::exit(1);
Expand Down Expand Up @@ -222,48 +183,3 @@ fn merge_compress(repo_path: &String, opts: &CompressAction) {

println!("successfully merge repo: {}", repo.id);
}

fn trans_go2rust(repo_path: &String) {
let run = || {
// get the repo
let repo = repo::get_repo(repo_path, true);
if let Err(err) = repo {
println!("get repo error: {:?}", err);
process::exit(1);
}

let mut repo = repo.unwrap();

// export the compress
println!("export repo: {}", repo.id);

let mut m = CodeCache::new(format!("go2rust_{}", &repo.id));
m.load_from_cache();
let out_dir = Path::new(&CONFIG.repo_dir).join("go2rust").join(&repo.id);

// block on convert_go2rust
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(convert_go2rust(
&mut repo,
out_dir.to_str().unwrap(),
&mut m,
));

// save the compressed repo
repo.save_to_cache();

println!("successfully compressed repo: {}", repo.id);
};

loop {
let result = panic::catch_unwind(run);
if let Err(err) = result {
println!("panic: {:?}", err);
// sleep 60s and retry
thread::sleep(Duration::from_secs(60));
continue;
} else {
process::exit(0);
}
}
}
Loading