Commit 2696cac3 authored by Justus's avatar Justus Committed by Jeffrey I. Schiller

Rewrite Extensions packaging scripts

Change-Id: I0a5c96a31fd7661ca0a68049f5d7719801998206
parent 7b73d553
......@@ -74,7 +74,7 @@ public final class Compiler {
// Copied from SdkLevel.java (which isn't in our class path so we duplicate it here)
private static final String LEVEL_GINGERBREAD_MR1 = "10";
public static final String RUNTIME_FILES_DIR = "/files/";
public static final String RUNTIME_FILES_DIR = SLASH + "files" + SLASH;
// Build info constants. Used for permissions, libraries and assets.
// Must match ComponentProcessor.ARMEABI_V7A_SUFFIX
......
This diff is collapsed.
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2015 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.scripts;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.util.Properties;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.*;
import java.io.File;
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* Tool to generate component.build_info.json for extensions.
* And put the libraries used by the extension in the correct directory for packaging.
*
* @author mouha.oumar@gmail.com (Mouhamadou O. Sall)
*/
public class ExternalComponentBuildInfoGenerator {
/**
* The definitions of the arguments used by this script
*
* args[0]: the path to simple_component_build_info.json
* args[1]: the path to external_components.txt
* args[2]: the path to ExternalComponents folder
* args[3]: the path to /build/classes/BuildServer/files
* args[4]: the path to external componentsTemp directory
* args[5]: the path to simple_component.json
*/
public static void main(String[] args) throws IOException, ParseException, JSONException {
JSONParser parser = new JSONParser();
String simple_component_build_text = readFile(args[0], Charset.defaultCharset());
Object simple_component_build_obj = parser.parse(simple_component_build_text);
JSONArray simple_component_build_info_array = (JSONArray) simple_component_build_obj;
ArrayList<String> components = fileToArray(args[1]);
for (int i = 0; i < simple_component_build_info_array.size(); i++) {
JSONObject component = (JSONObject)simple_component_build_info_array.get(i);
String componentType = component.get("type").toString();
JSONArray libraries = (JSONArray) component.get("libraries");
String componentName = componentType.substring(componentType.lastIndexOf(".") + 1);
String componentFileDirectory = args[2]+File.separator+ componentName+ File.separator+"files";
if(components.contains(componentName)) {
// Rename "libraries" to "externallibraries"
// We will use "externallibraries" later in the build process so
// we know which libraries we need to extract so they get "JarJar'd" into
// AndroidRuntime.jar. The Buildserver looks at "libraries" so we need to
// make sure it is an empty JSON array.
component.put("externallibraries", libraries);
component.put("libraries", new JSONArray());
new File(componentFileDirectory).mkdirs();
FileWriter file = new FileWriter(componentFileDirectory + File.separator + "component_build_info.json");
try {
file.write(component.toJSONString());
System.out.println("Successfully created component_build_info.json for "+componentName);
} catch (IOException e) {
e.printStackTrace();
} finally {
file.flush();
file.close();
}
// Copying related libraries to a given extension into his files folder
JSONArray libraryArray = (JSONArray)component.get("externallibraries");
for(int j = 0; j<libraryArray.size();j++){
Object library = libraryArray.get(j);
copyFile(new File(args[3]+File.separator+library.toString()),
new File(componentFileDirectory+File.separator+library.toString()));
}
// Copying ComponentName.jar into his files folder
copyFile(new File(args[4]+File.separator+componentName+".jar"),
new File(componentFileDirectory+File.separator+"AndroidRuntime.jar"));
//Adding extension.properties
String componentDirectory = args[2] + File.separator + componentName;
StringBuilder sb = new StringBuilder();
sb.append("type=external\n");
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(componentDirectory + File.separator + "extension.properties")));
writer.write(sb.toString());
writer.flush();
writer.close();
//Renaming folder accordingly
File extensionDir = new File(args[2] + File.separator + componentName);
File newExtensionDir = new File(args[2] + File.separator + componentType);
extensionDir.renameTo(newExtensionDir);
}
}
}
/**
* Read a file and returns its content
*
* @param path the path of the file to be read
* @param encoding the encoding system
*/
private static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
/**
* Read external_components.txt (a file containing the name of the extension)
* and returns an ArrayList containing those components
*
* @param fileName the path of the file to be read
*/
private static ArrayList<String> fileToArray(String fileName) throws FileNotFoundException{
Scanner sc = new Scanner(new File(fileName));
ArrayList<String> components = new ArrayList<String>();
while (sc.hasNextLine()) {
components.add(sc.nextLine());
}
return components;
}
/**
* Copy a file from a given source to a given destination
*
* @param source the file to be copied
* @param dest where the file will be copied
*/
private static void copyFile(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
/**
* Delete a directory
*
* Code posted on http://stackoverflow.com/questions/3775694/deleting-folder-from-java
* @param directory the folder to be removed
*/
public static boolean deleteDirectory(File directory) {
if(directory.exists()){
File[] files = directory.listFiles();
if(null!=files){
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
}
return(directory.delete());
}
}
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2015 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.scripts;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.io.File;
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* Tool to generate component.json for extension.
*
* @author mouha.oumar@gmail.com (Mouhamadou O. Sall)
*/
public class ExternalComponentJsonGenerator {
/**
* The definitions of the arguments used by this script
*
* args[0]: the path to simple_component.json
* args[1]: the path to external_components.txt
* args[2]: the path to ExternalComponentAsset.dir: "${local.build.dir}/ExternalComponents"
*/
public static void main(String[] args) throws IOException, ParseException, JSONException {
JSONParser parser = new JSONParser();
String simple_component_text = readFile(args[0],Charset.defaultCharset());
Object simple_component_obj = parser.parse(simple_component_text);
JSONArray simple_component_array = (JSONArray)simple_component_obj;
ArrayList<String> components = fileToArray(args[1]);
for(int i = 0; i<simple_component_array.size(); i++){
JSONObject component = (JSONObject)simple_component_array.get(i);
if(components.contains(component.get("name"))){ //TODO(Mos): Should test the external boolean here instead
new File(args[2]+"/"+component.get("name").toString()).mkdirs();
FileWriter file = new FileWriter(args[2]+"/"+component.get("name")+"/"+"component.json");
try {
file.write(component.toJSONString());
System.out.println("Successfully created "+ component.get("name") +" JSON Object to File...");
} catch (IOException e) {
e.printStackTrace();
} finally {
file.flush();
file.close();
}
}
}
}
/**
* Read a file and returns its content
*
* @param path the path of the file to be read
* @param encoding the encoding system
*/
private static String readFile(String path, Charset encoding) throws IOException{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
/**
* Read external_components.txt (a file containing the name of the extension)
* and returns an ArrayList containing those components
*
* @param fileName the path of the file to be read
*/
private static ArrayList<String> fileToArray(String fileName) throws FileNotFoundException{
Scanner sc = new Scanner(new File(fileName));
ArrayList<String> components = new ArrayList<String>();
while (sc.hasNextLine()) {
components.add(sc.nextLine());
}
return components;
}
}
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2015 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.scripts;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.io.BufferedWriter;
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* Tool to generate the list of the extension.
*
* @author mouha.oumar@gmail.com (Mouhamadou O. Sall)
*/
public class ExternalComponentListGenerator {
/**
* The definitions of the arguments used by this script
*
* args[0]: the path to simple_component_build_info.json
* args[1]: the path to ExternalComponents folder
*/
public static void main(String[] args) throws IOException, ParseException, JSONException {
JSONParser parser = new JSONParser();
String simple_component_build_info_text = readFile(args[0],Charset.defaultCharset());
Object simple_component_build_info_obj = parser.parse(simple_component_build_info_text);
JSONArray simple_component_build_info_array = (JSONArray)simple_component_build_info_obj;
ArrayList<String> externalComponents = new ArrayList<String>();
for(int i = 0; i<simple_component_build_info_array.size();i++){
JSONObject component = (JSONObject)simple_component_build_info_array.get(i);
if(component.get("categoryString").toString().equals("EXTENSION")){ // TODO(Mos): Should test the external boolean here instead ?
externalComponents.add(component.get("name").toString());
}
}
write(args[1] + "/" + "external_components.txt",externalComponents);
}
public static void write (String filename, ArrayList<String> components) throws IOException{
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(filename));
for (int j = 0; j<components.size(); j++) {
outputWriter.write(components.get(j));
outputWriter.newLine();
}
outputWriter.flush();
outputWriter.close();
}
private static String readFile(String path, Charset encoding) throws IOException{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2015 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.scripts;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.*;
import java.io.File;
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* Tool to create the folder structure for packaging the extensions.
*
* @author mouha.oumar@gmail.com (Mouhamadou O. Sall)
*/
public class ExternalComponentPackaging {
/**
* The definitions of the arguments used by this script
*
* args[0]: the path to simple_component_build_info.json
* args[1]: the path to external_components.txt
* args[2]: the path to "${AndroidRuntime-class.dir}"
* args[3]: the path to /build/classes/BuildServer/files
* args[4]: the path to ExternalComponentTemp directory
* args[5]: the path to simple_component.json
*/
public static void main(String[] args) throws IOException, ParseException, JSONException {
JSONParser parser = new JSONParser();
ArrayList<String> components = fileToArray(args[1]);
// Copying related libraries to a given extension into his files folder in (ExternalComponentTemp dir)
String simple_component_build_info_text = readFile(args[0], Charset.defaultCharset());
Object simple_component_build_info_obj = parser.parse(simple_component_build_info_text);
JSONArray simple_component_build_info_array = (JSONArray) simple_component_build_info_obj;
for (int i = 0; i < simple_component_build_info_array.size(); i++) {
JSONObject component = (JSONObject) simple_component_build_info_array.get(i);
String componentType = component.get("type").toString();
String componentName = componentType.substring(componentType.lastIndexOf(".") + 1);
if(components.contains(componentName)) {
String componentTempDirectory = args[4]+File.separator+ componentName;
new File(componentTempDirectory).mkdirs();
JSONArray libraryArray = (JSONArray)component.get("libraries");
for(int j = 0; j<libraryArray.size();j++){
Object library = libraryArray.get(j);
copyFile(new File(args[3]+File.separator+library.toString()),
new File(componentTempDirectory+File.separator+library.toString()));
}
}
}
// Copying related compiled files to a given extension into his package folder in (ExternalComponentTemp dir)
String simple_component_text = readFile(args[5], Charset.defaultCharset());
Object simple_component_obj = parser.parse(simple_component_text);
JSONArray simple_component_array = (JSONArray) simple_component_obj;
for (int i = 0; i < simple_component_array.size(); i++) {
JSONObject component = (JSONObject) simple_component_array.get(i);
if(components.contains(component.get("name"))) {
String componentClassPath = component.get("type").toString();
componentClassPath = componentClassPath.substring(0,componentClassPath.lastIndexOf(".")).replace('.','/');
String componentTempDirectory = args[4]+File.separator+ component.get("name");
String componentClassPathDirectory = componentTempDirectory + File.separator+componentClassPath;
new File(componentClassPathDirectory).mkdirs();
copyRelatedExternalClasses(new File(args[2]),component.get("name").toString(),componentClassPathDirectory);
}
}
}
/**
* Read a file and returns its content
*
* @param path the path of the file to be read
* @param encoding the encoding system
*/
private static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
/**
* Read external_components.txt (a file containing the name of the extension)
* and returns an ArrayList containing those components
*
* @param fileName the path of the file to be read
*/
private static ArrayList<String> fileToArray(String fileName) throws FileNotFoundException{
Scanner sc = new Scanner(new File(fileName));
ArrayList<String> components = new ArrayList<String>();
while (sc.hasNextLine()) {
components.add(sc.nextLine());
}
return components;
}
/**
* Copy a file from a given source to a given destination
*
* @param source the file to be copied
* @param dest where the file will be copied
*/
private static void copyFile(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
/**
* Copy a compiled classes related to a given extension in his package folder
*
* @param srcfolder the folder in which to check compiled classes
* @param externalComponentName the name of the extension
* @param destPath where the compiled classes will be copied
*/
private static void copyRelatedExternalClasses(final File srcfolder, String externalComponentName, final String destPath) throws IOException {
for (File fileEntry : srcfolder.listFiles()){
if (fileEntry.isFile()) {
if (fileEntry.getName().startsWith(externalComponentName)){
System.out.println(fileEntry.toString());
copyFile(fileEntry,new File (destPath+File.separator+fileEntry.getName()));
}
} else if (fileEntry.isDirectory()) {
copyRelatedExternalClasses(new File(fileEntry.getAbsolutePath()),externalComponentName, destPath);
}
}
}
}
JSON in Java [package org.json]
This package needs a new owner. I have not used it in over a decade, and I do
not have time to maintain programs that I do not use.
If you think you can give this package a good home, please contact me.
Douglas Crockford
douglas@crockford.com
2015-02-06
JSON is a light-weight, language independent, data interchange format.
See http://www.JSON.org/
The files in this package implement JSON encoders/decoders in Java.
It also includes the capability to convert between JSON and XML, HTTP
headers, Cookies, and CDL.
This is a reference implementation. There is a large number of JSON packages
in Java. Perhaps someday the Java community will standardize on one. Until
then, choose carefully.
The license includes this restriction: "The software shall be used for good,
not evil." If your conscience cannot live with that, then choose a different
package.
The package compiles on Java 1.8.
JSONObject.java: The JSONObject can parse text from a String or a JSONTokener
to produce a map-like object. The object provides methods for manipulating its
contents, and for producing a JSON compliant object serialization.
JSONArray.java: The JSONObject can parse text from a String or a JSONTokener
to produce a vector-like object. The object provides methods for manipulating
its contents, and for producing a JSON compliant array serialization.
JSONTokener.java: The JSONTokener breaks a text into a sequence of individual
tokens. It can be constructed from a String, Reader, or InputStream.
JSONException.java: The JSONException is the standard exception type thrown
by this package.
JSONString.java: The JSONString interface requires a toJSONString method,
allowing an object to provide its own serialization.
JSONStringer.java: The JSONStringer provides a convenient facility for
building JSON strings.
JSONWriter.java: The JSONWriter provides a convenient facility for building
JSON text through a writer.
CDL.java: CDL provides support for converting between JSON and comma
delimited lists.
Cookie.java: Cookie provides support for converting between JSON and cookies.
CookieList.java: CookieList provides support for converting between JSON and
cookie lists.
HTTP.java: HTTP provides support for converting between JSON and HTTP headers.
HTTPTokener.java: HTTPTokener extends JSONTokener for parsing HTTP headers.
XML.java: XML provides support for converting between JSON and XML.
JSONML.java: JSONML provides support for converting between JSONML and XML.
XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text.
\ No newline at end of file
This diff is collapsed.
Please visit:
http://code.google.com/p/json-simple/
\ No newline at end of file
package org.json.simple.parser;
%%
%{
private StringBuffer sb=new StringBuffer();
int getPosition(){
return yychar;
}
%}
%table
%unicode
%state STRING_BEGIN
%yylexthrow ParseException
%char
HEX_D = [a-fA-F0-9]
INT = [-]?[0-9]+
DOUBLE = {INT}((\.[0-9]+)?([eE][-+]?[0-9]+)?)
WS = [ \t\r\n]
UNESCAPED_CH = [^\"\\]
FALLBACK_CH = .
%%
<STRING_BEGIN> \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE, sb.toString());}
<STRING_BEGIN> {UNESCAPED_CH}+ { sb.append(yytext());}
<STRING_BEGIN> \\\" {sb.append('"');}
<STRING_BEGIN> \\\\ {sb.append('\\');}
<STRING_BEGIN> \\\/ {sb.append('/');}
<STRING_BEGIN> \\b {sb.append('\b');}
<STRING_BEGIN> \\f {sb.append('\f');}
<STRING_BEGIN> \\n {sb.append('\n');}
<STRING_BEGIN> \\r {sb.append('\r');}
<STRING_BEGIN> \\t {sb.append('\t');}
<STRING_BEGIN> \\u{HEX_D}{HEX_D}{HEX_D}{HEX_D} { try{
int ch=Integer.parseInt(yytext().substring(2),16);
sb.append((char)ch);
}
catch(Exception e){
throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_EXCEPTION, e);
}
}
<STRING_BEGIN> \\ {sb.append('\\');}
<YYINITIAL> \" { sb = null; sb = new StringBuffer(); yybegin(STRING_BEGIN);}
<YYINITIAL> {INT} { Long val=Long.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);}
<YYINITIAL> {DOUBLE} { Double val=Double.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);}
<YYINITIAL> "true"|"false" { Boolean val=Boolean.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);}
<YYINITIAL> "null" { return new Yytoken(Yytoken.TYPE_VALUE, null);}
<YYINITIAL> "{" { return new Yytoken(Yytoken.TYPE_LEFT_BRACE,null);}
<YYINITIAL> "}" { return new Yytoken(Yytoken.TYPE_RIGHT_BRACE,null);}
<YYINITIAL> "[" { return new Yytoken(Yytoken.TYPE_LEFT_SQUARE,null);}
<YYINITIAL> "]" { return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE,null);}
<YYINITIAL> "," { return new Yytoken(Yytoken.TYPE_COMMA,null);}
<YYINITIAL> ":" { return new Yytoken(Yytoken.TYPE_COLON,null);}
<YYINITIAL> {WS}+ {}
<YYINITIAL> {FALLBACK_CH} { throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_CHAR, new Character(yycharat(0)));}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment