3
3
use crate :: gen:: RpcDocGenerator ;
4
4
use ckb_rpc:: module:: * ;
5
5
use serde_json:: json;
6
- use std:: fs ;
6
+ use std:: { fs , path :: PathBuf } ;
7
7
8
8
const OPENRPC_DIR : & str = "./docs/ckb_rpc_openrpc/" ;
9
9
@@ -21,35 +21,41 @@ fn run_command(prog: &str, args: &[&str], dir: Option<&str>) -> Option<String> {
21
21
} )
22
22
}
23
23
24
- fn get_tag ( ) -> String {
25
- run_command ( "git" , & [ "describe" , "--tags" , "--abbrev=0" ] , None ) . unwrap_or ( "main" . to_owned ( ) )
24
+ fn get_version ( ) -> String {
25
+ let version = run_command ( "cargo" , & [ "pkgid" ] , None )
26
+ . unwrap ( )
27
+ . split ( '#' )
28
+ . nth ( 1 )
29
+ . unwrap_or ( "0.0.0" )
30
+ . to_owned ( ) ;
31
+ eprintln ! ( "version: {:?}" , version) ;
32
+ return version;
26
33
}
27
34
28
35
/// Get git tag from command line
29
36
fn get_commit_sha ( ) -> String {
30
37
run_command ( "git" , & [ "rev-parse" , "HEAD" ] , Some ( OPENRPC_DIR ) ) . unwrap_or ( "main" . to_string ( ) )
31
38
}
32
39
33
- fn checkout_tag_branch ( tag : & str ) {
40
+ fn checkout_tag_branch ( version : & str ) {
34
41
let dir = Some ( OPENRPC_DIR ) ;
35
- let res = run_command ( "git" , & [ "checkout" , tag ] , dir) ;
42
+ let res = run_command ( "git" , & [ "checkout" , version ] , dir) ;
36
43
if res. is_none ( ) {
37
- run_command ( "git" , & [ "checkout" , "-b" , tag ] , dir) ;
44
+ run_command ( "git" , & [ "checkout" , "-b" , version ] , dir) ;
38
45
}
39
46
}
40
47
41
48
fn dump_openrpc_json ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
42
- let dir = "./docs/ckb_rpc_openrpc/json/" ;
43
- let tag = get_tag ( ) ;
44
- checkout_tag_branch ( & tag) ;
45
-
46
- fs:: create_dir_all ( dir) ?;
49
+ let json_dir = PathBuf :: from ( OPENRPC_DIR ) . join ( "json" ) ;
50
+ let version: String = get_version ( ) ;
51
+ checkout_tag_branch ( & version) ;
52
+ fs:: create_dir_all ( & json_dir) ?;
47
53
let dump =
48
54
|name : & str , doc : & mut serde_json:: Value | -> Result < ( ) , Box < dyn std:: error:: Error > > {
49
- doc[ "info" ] [ "version" ] = serde_json:: Value :: String ( tag . clone ( ) ) ;
55
+ doc[ "info" ] [ "version" ] = serde_json:: Value :: String ( version . clone ( ) ) ;
50
56
let obj = json ! ( doc) ;
51
57
let res = serde_json:: to_string_pretty ( & obj) ?;
52
- fs:: write ( dir . to_owned ( ) + name, res) ?;
58
+ fs:: write ( json_dir . join ( name) , res) ?;
53
59
Ok ( ( ) )
54
60
} ;
55
61
dump ( "alert_rpc_doc.json" , & mut alert_rpc_doc ( ) ) ?;
@@ -68,7 +74,7 @@ fn dump_openrpc_json() -> Result<(), Box<dyn std::error::Error>> {
68
74
dump ( "experiment_rpc_doc.json" , & mut experiment_rpc_doc ( ) ) ?;
69
75
eprintln ! (
70
76
"finished dump openrpc json for tag: {:?} at dir: {:?}" ,
71
- tag , dir
77
+ version , json_dir
72
78
) ;
73
79
Ok ( ( ) )
74
80
}
@@ -89,8 +95,8 @@ pub fn gen_rpc_readme(readme_path: &str) -> Result<(), Box<dyn std::error::Error
89
95
experiment_rpc_doc( ) ,
90
96
] ;
91
97
92
- let tag = get_commit_sha ( ) ;
93
- let generator = RpcDocGenerator :: new ( & all_rpc, readme_path. to_owned ( ) , tag ) ;
98
+ let commit_sha = get_commit_sha ( ) ;
99
+ let generator = RpcDocGenerator :: new ( & all_rpc, readme_path. to_owned ( ) , commit_sha ) ;
94
100
fs:: write ( readme_path, generator. gen_markdown ( ) ) ?;
95
101
96
102
Ok ( ( ) )
0 commit comments