Sencha Touch 教你一键打包+发布

思路介绍:css

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

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

  3. 将resources下面的图片所有放进缓存;web

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

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

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

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

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

  9. 你能够加入交流群:377140502app

  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.效果图:

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

相关文章
相关标签/搜索