Skip to content

Commit 8383b7f

Browse files
committed
feat: support export action
1 parent 9a1696d commit 8383b7f

File tree

10 files changed

+296
-154
lines changed

10 files changed

+296
-154
lines changed

src/bin/cmd.rs

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use std::{env, panic, process, thread, time::Duration};
1616

1717
use ABCoder::{
1818
compress::compress::compress_all,
19-
repo::{self, CompressOptions},
19+
export::{self, ExportOptions},
20+
parse::{self, CompressOptions},
2021
};
2122

2223
#[derive(Clone, Debug)]
@@ -27,25 +28,8 @@ struct Options {
2728

2829
#[derive(Clone, Debug)]
2930
enum Action {
30-
Compress(CompressAction),
31-
}
32-
33-
#[derive(Clone, Debug, Default)]
34-
struct CompressAction {
35-
export_compress: bool,
36-
force_update_ast: bool,
37-
not_load_external_symbol: bool,
38-
no_need_comment: bool,
39-
}
40-
41-
impl CompressAction {
42-
fn to_compress_options(&self) -> CompressOptions {
43-
CompressOptions {
44-
export_compress: self.export_compress,
45-
not_load_external_symbol: self.not_load_external_symbol,
46-
no_need_comment: self.no_need_comment,
47-
}
48-
}
31+
Compress(CompressOptions),
32+
Export(ExportOptions),
4933
}
5034

5135
fn main() {
@@ -56,23 +40,28 @@ fn main() {
5640
match options.action {
5741
Action::Compress(cmp) => {
5842
if cmp.force_update_ast {
59-
merge_compress(&options.repo_path, &cmp);
43+
merge_repo(&options.repo_path, &cmp);
6044
}
6145
compress(&options.repo_path, &cmp);
62-
if cmp.export_compress {
63-
export_compress(&options.repo_path, &cmp);
64-
}
46+
}
47+
Action::Export(exp) => {
48+
export(&options.repo_path, &exp);
6549
}
6650
}
6751
}
6852

6953
const USAGE: &str = "Usage: ABCoder <Action> <RepoPath> [Flags]
70-
Action: compress
71-
compress: compress the repo. Including flags:
72-
--export-compress: export the compress result
73-
--force-update-ast: force parsing repo and merge the previous result
74-
--not-load-external-symbol: not load external external symbols to speed up parsing
75-
--no-need-comment: not need comment in symbol content (only works for Go now)
54+
Actions:
55+
compress: compress the repo. Including flags:
56+
--parse-only: only parse the repo, not compress it
57+
--export-compress: export the compress result
58+
--force-update-ast: force parsing repo and merge the previous result
59+
--not-load-external-symbol: not load external external symbols to speed up parsing
60+
--no-need-comment: not need comment in symbol content (only works for Go now)
61+
export: export the compress result to csv or markdown (default). Including flags:
62+
--csv: export the compress result to csv
63+
--out-dir <path>: output directory path, default is current directory
64+
--public-only: only export the public symbols
7665
";
7766

7867
fn parse_options() -> Options {
@@ -84,13 +73,10 @@ fn parse_options() -> Options {
8473

8574
let action = match args[1].as_str() {
8675
"compress" => {
87-
let mut compress_action = CompressAction::default();
76+
let mut compress_action = CompressOptions::default();
8877
if args.len() > 3 {
8978
for i in 3..args.len() {
9079
match args[i].as_str() {
91-
"--export-compress" => {
92-
compress_action.export_compress = true;
93-
}
9480
"--force-update-ast" => {
9581
compress_action.force_update_ast = true;
9682
}
@@ -104,10 +90,34 @@ fn parse_options() -> Options {
10490
}
10591
}
10692
}
107-
10893
Action::Compress(compress_action)
10994
}
11095

96+
"export" => {
97+
let mut opts = ExportOptions::default();
98+
if args.len() > 3 {
99+
for i in 3..args.len() {
100+
match args[i].as_str() {
101+
"--out-dir" => {
102+
if args.len() <= i + 1 {
103+
println!("--out-dir must specify a value");
104+
process::exit(1);
105+
}
106+
opts.output = Some(args[i + 1].clone());
107+
}
108+
"--csv" => {
109+
opts.csv = true;
110+
}
111+
"--public-only" => {
112+
opts.public_only = true;
113+
}
114+
_ => {}
115+
}
116+
}
117+
}
118+
Action::Export(opts)
119+
}
120+
111121
_ => {
112122
println!("{}", USAGE);
113123
process::exit(1);
@@ -120,17 +130,22 @@ fn parse_options() -> Options {
120130
}
121131
}
122132

123-
fn compress(repo_path: &String, cmp: &CompressAction) {
133+
fn compress(repo_path: &String, cmp: &CompressOptions) {
124134
// recoverable logic
125135
let run = || {
126136
// get the repo
127-
let repo = repo::get_repo(repo_path, &cmp.to_compress_options());
137+
let repo = parse::get_repo(repo_path, &cmp);
128138
if let Err(err) = repo {
129139
println!("get repo error: {:?}", err);
130140
process::exit(1);
131141
}
132142

133143
let mut repo = repo.unwrap();
144+
repo.save_to_cache();
145+
println!("successfully parsed repo: {}", repo.id);
146+
if cmp.parse_only {
147+
return;
148+
}
134149

135150
// compress the repo
136151
println!("compressing repo: {}", repo.id);
@@ -157,9 +172,9 @@ fn compress(repo_path: &String, cmp: &CompressAction) {
157172
}
158173
}
159174

160-
fn export_compress(repo_path: &String, cmp: &CompressAction) {
175+
fn export(repo_path: &String, cmp: &ExportOptions) {
161176
// get the repo
162-
let repo = repo::get_repo(repo_path, &cmp.to_compress_options());
177+
let repo = parse::get_repo(repo_path, &CompressOptions::default());
163178
if let Err(err) = repo {
164179
println!("get repo error: {:?}", err);
165180

@@ -170,24 +185,22 @@ fn export_compress(repo_path: &String, cmp: &CompressAction) {
170185

171186
// export the compress
172187
println!("export repo: {}", repo.id);
173-
repo::export_repo(&mut repo);
174-
// save the compressed repo
175-
repo.save_to_cache();
188+
export::export_repo(&mut repo, cmp);
176189

177-
println!("successfully compressed repo: {}", repo.id);
190+
println!("successfully exported repo: {}", repo.id);
178191
}
179192

180-
fn merge_compress(repo_path: &String, cmp: &CompressAction) {
193+
fn merge_repo(repo_path: &String, cmp: &CompressOptions) {
181194
// get old repo
182-
let repo = repo::get_repo(repo_path, &cmp.to_compress_options());
195+
let repo = parse::get_repo(repo_path, &cmp);
183196
if let Err(err) = repo {
184197
println!("get repo error: {:?}", err);
185198
process::exit(1);
186199
}
187200
let mut repo = repo.unwrap();
188201

189202
// parse new repo
190-
let nrepo = repo::force_parse_repo(repo_path, &cmp.to_compress_options());
203+
let nrepo = parse::force_parse_repo(repo_path, &cmp);
191204
if let Err(err) = nrepo {
192205
println!("parse repo error: {:?}", err);
193206
process::exit(1);

src/compress/types/types.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ impl<'de> Deserialize<'de> for NodeType {
127127
}
128128

129129
impl Repository {
130+
pub fn is_external_mod(&self, mod_path: &str) -> bool {
131+
return mod_path.contains("@") || mod_path == "std" || mod_path == "";
132+
}
133+
130134
pub fn get_id_content(&self, id: &Identity) -> Option<String> {
131135
if let Some(m) = self.modules.get(&id.mod_path) {
132136
if let Some(pkg) = m.packages.get(&id.pkg_path) {
@@ -354,6 +358,8 @@ pub struct Module {
354358
pub packages: HashMap<String, Package>,
355359
#[serde(rename = "Files")]
356360
pub files: HashMap<String, File>,
361+
#[serde(rename = "Language")]
362+
pub language: String,
357363
}
358364

359365
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -388,7 +394,7 @@ pub struct Package {
388394
pub types: HashMap<String, Struct>,
389395
#[serde(rename = "Vars")]
390396
pub vars: HashMap<String, Variant>,
391-
397+
#[serde(rename = "compress_data")]
392398
pub compress_data: Option<String>,
393399
}
394400

@@ -556,6 +562,7 @@ pub struct Variant {
556562
pub content: String,
557563

558564
// compress_data
565+
#[serde(rename = "compress_data")]
559566
pub compress_data: Option<String>,
560567
}
561568

@@ -611,6 +618,7 @@ pub struct Function {
611618
pub global_vars: Option<Vec<Identity>>,
612619

613620
// compress_data
621+
#[serde(rename = "compress_data")]
614622
pub compress_data: Option<String>,
615623
}
616624

@@ -653,7 +661,7 @@ pub struct Struct {
653661
#[serde(rename = "Exported", default)]
654662
pub is_exported: bool,
655663
#[serde(rename = "TypeKind")]
656-
type_kind: u8,
664+
pub type_kind: String,
657665
#[serde(rename = "Content")]
658666
pub(crate) content: String,
659667
#[serde(rename = "SubStruct")]
@@ -664,6 +672,7 @@ pub struct Struct {
664672
pub(crate) methods: Option<HashMap<String, Identity>>,
665673

666674
// compress_data
675+
#[serde(rename = "compress_data")]
667676
pub compress_data: Option<String>,
668677
}
669678

src/config/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::path::{Path, PathBuf};
1717
use lazy_static::lazy_static;
1818
use serde::Deserialize;
1919

20-
use crate::repo::{self, CompressOptions};
20+
use crate::parse;
2121

2222
#[derive(Debug)]
2323
pub enum Language {
@@ -218,7 +218,10 @@ fn decide_language(path: &str) -> ProgramLanguage {
218218
.unwrap_or(ProgramLanguage::Unknown(path.to_string()))
219219
}
220220

221-
pub fn parser_and_args<'a>(repo_path: &'a str, opts: &CompressOptions) -> (String, Vec<String>) {
221+
pub fn parser_and_args<'a>(
222+
repo_path: &'a str,
223+
opts: &parse::CompressOptions,
224+
) -> (String, Vec<String>) {
222225
let lang = decide_language(repo_path);
223226
let path = rust_ast_path();
224227
let mut args = vec![

0 commit comments

Comments
 (0)