Sencha Touch 教你一键打包+发布

思路介绍:

  1. cmd生成新的工程文件(以下简称目标工程);

  2. 将当前正在开发的项目的app下面的文件copy到目标工程,并在copy过程中将对应的文件名放到app.json和bootstrap.json里面,将resources下面的文件全部移动到目标工程并将对应的css文件名放到app.json和bootstrap.json里面,并将lib下面的第三方js文件放到app.json下面一遍打包后能被项目找到;

  3. 将resources下面的图片全部放进缓存;

  4. 通过当前项目的app.js改变目标项目的app.js;

  5. 修改index.html文件(因为有时候我们会在项目中用到第三方的js包并在引入是配置相关参数,这时候我们就难免会在index.html中加入对应的js文件,比如百度地图的js文件需要配置key等等);

  6. 由于cmd打包本地应用还存在问题。我们本版本就先忽略本地应用,主要打包类型为production,package;

  7. 用java执行cmd打包的bat命令;

  8. 源码下载地址:http://pan.baidu.com/s/1hq1IFzu

  9. 你可以加入交流群:377140502

  10. 部分关键代码如下:

/**
 * 比对和转换app.json和bootstrap.json
 * @author AvenGang
 *
 */
public class App_bootstrap_JSON {

	/**
	 * 单例
	 * @return
	 */
	public static App_bootstrap_JSON getInstance() {
		return abj;
	}
	
	/**
	 * 替换app.json和bootstrap.json
	 * @param tag 目标路径
	 * @throws IOException
	 */
	public void replace(String tag) throws IOException {
		//找到目标项目的app.json文件和bootstrap.json
		File tarFile = new File(tag + File.separator + "app.json");
		File tarBootstrapFile = new File(tag + File.separator + "bootstrap.json");
		String jsStr = "";
		if(js.lastIndexOf(",") > -1) {
			jsStr = js.substring(0, js.lastIndexOf(","));//去掉多余的逗号
		}
		jsStr += "\r\n],\r\n";//添加结尾
		String cssStr = "";
		if(css.lastIndexOf(",") > -1) {
			cssStr = css.substring(0, css.lastIndexOf(","));//去掉多余的逗号
		}
		String bootstrapCssStr = cssStr + "\r\n]\r\n";
		
		cssStr += "\r\n],\r\n";//添加结尾
		String appCacheStr = "";
		if(appCache.lastIndexOf(",") > -1) {
			appCacheStr = appCache.substring(0, appCache.lastIndexOf(","));//去掉多余的逗号
		}
		//添加结尾
		appCacheStr += "\r\n\t" +
							"],\r\n\t" +
							"\"network\": [\r\n\t\t" +
				            	"\"*\"\r\n\t" +
					        "],\r\n\t" +
					        "\"fallback\": []\r\n" +
				        "},\r\n";
		
		String jsonStr = FileUtil.file2Str(tarFile);
		String preStr = jsonStr.substring(0, jsonStr.indexOf("\"js\":"));
		String postStr = jsonStr.substring(jsonStr.indexOf("\"ignore\":"));
		preStr += jsStr;
		preStr += cssStr;
		preStr += appCacheStr;
		preStr += resources;
		PrintWriter pw = new PrintWriter(tarFile);
		//app.json写数据
		pw.write(preStr+postStr);
		pw.close();
		String idStr = jsonStr.substring(jsonStr.indexOf("\"id\":"),jsonStr.length() - 1);
		PrintWriter pw1 = new PrintWriter(tarBootstrapFile);
		String str = "{"+idStr+","+jsStr+bootstrapCssStr+"}";
		//bootstrap.json写数据
		pw1.write(str);
		pw1.close();
	}
	
	public void addJs(String libJsName) {
		if(libJsName.contains(".svn")) {
			return;
		}
		if(!libJsName.endsWith(".js")) {
			return;
		}
		String str = "\r\n\t" +
							"{\r\n\t\t" +
								"\"path\" : \"resources/lib/%1$s\"\r\n\t" +
							"},";
		str = String.format(str, libJsName);
		js.append(str);
	}
	
	public void addCss(String cssName) {
		if(cssName.contains(".svn")) {
			return;
		}
		if(!cssName.endsWith(".css")) {
			return;
		}
		String str = "\r\n\t" +
							"{\r\n\t\t" +
								"\"path\" : \"resources/css/%1$s\"\r\n\t" +
							"},";
		str = String.format(str, cssName);
		css.append(str);
	}
	
	public void addAppCache(String cachePathName) {
		if(cachePathName.contains(".svn")) {
			return;
		}
		String str = "\"%1$s\",\r\n\t\t";
		str = String.format(str, cachePathName);
		appCache.append(str);
	}
	
	private App_bootstrap_JSON() {
		initJs();
		initCss();
		initAppCache();
		initResources();
	}
	
	/**
	 * 初始化,将系统不变的js文件配置到app.json里面sencha-touch.js
	 */
	private void initJs() {
		js.append("\"js\": [\r\n\t" +
						"{\r\n\t\t" +
							"\"path\": \"touch/sencha-touch.js\",\r\n\t\t" +
							"\"x-bootstrap\": true\r\n\t" +
						"},\r\n\t" +
						"{\r\n\t\t" +
							"\"path\": \"bootstrap.js\",\r\n\t\t" +
							"\"x-bootstrap\": true\r\n\t" +
						"},\r\n\t" +
						"{\r\n\t\t" +
							"\"path\": \"app.js\",\r\n\t\t" +
							"\"bundle\": true,\r\n\t\t" +
							"\"update\": \"full\"\r\n\t" +
						"},");
	}
	
	public void initCss() {
		css.append("\"css\": [\r\n\t");
	}
	
	public void initAppCache() {
		appCache.append("\"appCache\": {\r\n\t" +
								"\"cache\": [\r\n\t\t" +
									"\"index.html\",\r\n\t\t");
	}
	
	public void initResources() {
		resources.append("\"resources\": [\r\n\t");
			resources.append("\"resources/images\",\r\n\t\t");
			resources.append("\"resources/icons\",\r\n\t\t");
			resources.append("\"resources/startup\",\r\n\t\t");
			resources.append("\"resources/lib\"\r\n\t");
		resources.append("],");
	}
	
	private static StringBuffer js = new StringBuffer();
	private static StringBuffer css = new StringBuffer();
	private static StringBuffer appCache = new StringBuffer();
	private static StringBuffer resources = new StringBuffer();
	private static App_bootstrap_JSON abj = new App_bootstrap_JSON();
}


/**
 * 修改app.js文件
 * @author AvenGang
 *
 */
public class App_JS {

	public static void replace(String src, String tag) throws IOException {
		File tagFile = new File(tag + File.separator + "app.js");
		File srcFile = new File(src + File.separator + "app.js");
		String srcJsonStr = FileUtil.file2Str(srcFile);
		String tagJsonStr = FileUtil.file2Str(tagFile);
		String iconStr = tagJsonStr.substring(tagJsonStr.indexOf("icon:"))
				.split("}")[0] + "},";

		String str = "requires:['Ext.MessageBox',";
		if (srcJsonStr.indexOf("requires:[") > -1) {
			srcJsonStr = srcJsonStr.replace("requires:[",str);
		} else if (srcJsonStr.indexOf("requires :[") > -1) {
			srcJsonStr = srcJsonStr.replace("requires :[",str);
		} else if (srcJsonStr.indexOf("requires: [") > -1) {
			srcJsonStr = srcJsonStr.replace("requires: [",str);
		} else if (srcJsonStr.indexOf("requires : [") > -1) {
			srcJsonStr = srcJsonStr.replace("requires : [",str);
		} else {//没有required属性
			
		}

		String post_preStr = tagJsonStr.substring(
				tagJsonStr.indexOf("isIconPrecomposed:"),
				tagJsonStr.indexOf("launch:"));

		if (srcJsonStr.indexOf("icon:") > -1) {
			iconStr = srcJsonStr.substring(srcJsonStr.indexOf("icon:")).split(
					"}")[0]
					+ "},";
		} else if (srcJsonStr.indexOf("icon :") > -1) {
			iconStr = srcJsonStr.substring(srcJsonStr.indexOf("icon :")).split(
					"}")[0]
					+ "},";
		} else {
			iconStr = "";
		}
		String onUpdatedStr = tagJsonStr.substring(tagJsonStr
				.indexOf("onUpdated:"));
		
		String endStr = iconStr + post_preStr + onUpdatedStr;
		srcJsonStr.replace("});", endStr);
		
		PrintWriter pw = new PrintWriter(tagFile);
		pw.write(srcJsonStr);
		pw.close();
	}
}



/**
 * 修改index.html的内容
 * @author AvenGang
 *
 */
public class INDEX_HTML {

	public static void replace(String src, String tag) throws FileNotFoundException {
		File srcFile = new File(src + File.separator + "index.html");
		File tagFile = new File(tag + File.separator + "index.html");
		String srcJsonStr = FileUtil.file2Str(srcFile);
		ArrayList<String> lists = StringUtil.getValueBetweenTwoMarkers(srcJsonStr, "<!--cmd-->", "<!--/cmd-->");
		String tagJsonStr = FileUtil.file2Str(tagFile);
		StringBuffer sb = new StringBuffer();
		for(String str : lists) {
			sb.append(str+"\r\n");
		}
		tagJsonStr = tagJsonStr.replace("</head>", sb.toString() + "</head>");
		PrintWriter pw = new PrintWriter(tagFile);
		pw.write(tagJsonStr);
		pw.close();
	}
}

10.效果图:

091616_KMjs_259577.jpg

091616_CumP_259577.jpg

091617_tysw_259577.jpg

091617_56e8_259577.jpg

091618_VRhV_259577.jpg

091618_gh9z_259577.jpg

把这个文件夹放到tomcat等服务器下面就能在网上访问了,怎么样,是不是觉得比以前方便多了呢!

转载于:https://my.oschina.net/u/259577/blog/206148

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值