@@ -23,27 +23,26 @@ fn dist_client(nightly: bool) -> Result<()> {
2323 let _d = pushd ( "./editors/code" ) ;
2424
2525 let package_json_path = PathBuf :: from ( "./package.json" ) ;
26- let original_package_json = fs2:: read_to_string ( & package_json_path) ?;
27- let _restore =
28- Restore { path : package_json_path. clone ( ) , contents : original_package_json. clone ( ) } ;
26+ let mut patch = Patch :: new ( package_json_path. clone ( ) ) ?;
2927
3028 let date = run ! ( "date --utc +%Y%m%d" ) ?;
3129 let version_suffix = if nightly { "-nightly" } else { "" } ;
3230
33- let mut package_json = original_package_json . replace (
31+ patch . replace (
3432 r#""version": "0.2.20200309-nightly""# ,
3533 & format ! ( r#""version": "0.1.{}{}""# , date, version_suffix) ,
3634 ) ;
3735
3836 if nightly {
39- package_json = package_json . replace (
37+ patch . replace (
4038 r#""displayName": "rust-analyzer""# ,
41- r#""displayName": "rust-analyzer nightly""# ,
39+ r#""displayName": "rust-analyzer ( nightly) ""# ,
4240 ) ;
43- } else {
44- package_json = package_json. replace ( r#""enableProposedApi": true,"# , r#""# ) ;
4541 }
46- fs2:: write ( package_json_path, package_json) ?;
42+ if !nightly {
43+ patch. replace ( r#""enableProposedApi": true,"# , r#""# ) ;
44+ }
45+ patch. commit ( ) ?;
4746
4847 run ! ( "npm ci" ) ?;
4948 run ! ( "npx vsce package -o ../../dist/rust-analyzer.vsix" ) ?;
@@ -80,13 +79,31 @@ fn dist_server() -> Result<()> {
8079 Ok ( ( ) )
8180}
8281
83- struct Restore {
82+ struct Patch {
8483 path : PathBuf ,
84+ original_contents : String ,
8585 contents : String ,
8686}
8787
88- impl Drop for Restore {
88+ impl Patch {
89+ fn new ( path : PathBuf ) -> Result < Patch > {
90+ let contents = fs2:: read_to_string ( & path) ?;
91+ Ok ( Patch { path, original_contents : contents. clone ( ) , contents } )
92+ }
93+
94+ fn replace ( & mut self , from : & str , to : & str ) -> & mut Patch {
95+ assert ! ( self . contents. contains( from) ) ;
96+ self . contents = self . contents . replace ( from, to) ;
97+ self
98+ }
99+
100+ fn commit ( & self ) -> Result < ( ) > {
101+ fs2:: write ( & self . path , & self . contents )
102+ }
103+ }
104+
105+ impl Drop for Patch {
89106 fn drop ( & mut self ) {
90- fs2:: write ( & self . path , & self . contents ) . unwrap ( ) ;
107+ fs2:: write ( & self . path , & self . original_contents ) . unwrap ( ) ;
91108 }
92109}
0 commit comments