您的位置:黄埔信息网 >科普大全 > 插件清理(用Rust清理eclipse自动升级后的重复插件)

插件清理(用Rust清理eclipse自动升级后的重复插件)

时间:2021-04-16 10:30:53来源:黄埔信息网

插件清理(用Rust清理eclipse自动升级后的重复插件)用Rust清理eclipse自动升级后的重复插件

1. 简介

eclipse自动升级版本之后,在/eclipse/plugins目录仍然会保留旧版本的插件,想要写一个脚本清理插件,正好最近刚学习rust编程,便用rust开发了一个eclipse插件清理工具eclean。

本文简单介绍清理工具的开发过程,详细源代码可以在github下载并自行编译:

gitclonehttps://github.com/leexgone/ecleaner.gitcd./ecleanercargobuild--release

工具支持清理eclipse升级后plugins目录下的冗余插件。

清理eclipse插件目录并将清理插件备份:

ecleanc:\eclipsee:\backup\eclipse

检测eclipse目录下是否含有可清理的插件:

eclean-tc:\eclipse

更多命令可以查阅:

eclean--help

2.创建工程

使用cargo new elean创建工程,调整Cargo.toml内容并在src目录下创建lib.rs文件。

3.命令行参数解析

eclean是一个命令行工具,首先我们需要支持命令行参数的解析。

rust的clap库是一套功能强大的命令行参数解析库,这里我们使用clap解析命令行参数。

3.1 引用clap库

在Cargo.toml里加入clap依赖:

[dependencies]clap="2.33.3"

3.2 创建Config结构

编辑lib.rs代码,定义Config结构存储命令配置信息,使用clap解析命令参数:

usestd::{collections::HashMap,error::Error,fs,io::{self,ErrorKind},path::{Path,PathBuf},usize};usestd::fmt::Display;useclap::{App,Arg};pubstructConfig{dir:String,backup:String,verbose:bool,test:bool,force:bool,}implDisplayforConfig{fnfmt(&self,f:&mutstd::fmt::Formatter<'_>)->std::fmt::Result{write!(f,"[dir={},backup={},verbose={},test={},force={}]",self.dir,self.backup,self.verbose,self.test,self.force)}}implConfig{pubfnnew()->Result<Config,String>{letmatches=App::new("eclean").version("1.1.0").author("StevenLee<leexgone@163.com>").about("Cleanuptheduplicatedpluginsineclipsepluginsdirectory.").arg(Arg::with_name("DIR").help("Theeclipserootdirectorytobecleaned.The`/plugins`directoryshouldbeunderthisdirectory.").required(true).index(1)).arg(Arg::with_name("BACKUP").help("Specifyabackupdirectorytostoretheremovedplugins.").required_unless("test").index(2)).arg(Arg::with_name("verbose").short("v").long("verbose").help("Useverboseoutput")).arg(Arg::with_name("test").short("t").long("test").help("Scanandfindtheduplicatedplugins,butdonothing")).arg(Arg::with_name("force").short("f").long("force").help("Cleanuptheduplicatedpluginsautomatically.Neverprompt.")).get_matches();letdir=matches.value_of("DIR").unwrap();letbackup=matches.value_of("BACKUP").unwrap_or("");letverbose=matches.is_present("verbose");lettest=matches.is_present("test");letforce=matches.is_present("force");letroot_path=Path::new(dir);if!root_path.is_dir(){letmsg=format!("DIR'{}'doesnotexist",dir);returnErr(msg);}if!test{letbackup_path=Path::new(backup);if!backup_path.is_dir(){letmsg=format!("BACKUPdir'{}'doesnotexist",backup);returnErr(msg);}}Ok(Config{dir:String::from(dir),backup:String::from(backup),verbose,test,force,})}}

Config结构存储了用户参数命令的配置信息:

dir:eclipse目录(必须)

backup: 清理插件备份目录(必须,在test模式下可以忽略)

verbose:输出详细日志

test:仅检测eclipse插件目录,不执行清理操作

force:不询问用户强制清理插件

这里我们对用户的输入进行了检测,在目录不存在时提示错误;而必须参数的校验则通过clap完成即可。

关于我们| 广告服务| 诚聘英才| 联系我们| 友情链接| 免责申明| 网站地图
Powered by www.huangpujs.cn 版权所有