mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
feat: build apk from project(dir) support
This commit is contained in:
parent
316415343f
commit
c93c27fcaa
@ -1 +1 @@
|
||||
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":240},"path":"release-3.0.0 Alpha40.apk","properties":{"packageId":"com.stardust.scriptdroid","split":"","minSdkVersion":"17"}}]
|
||||
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":34},"path":"inrt-release.apk","properties":{"packageId":"com.stardust.auojs.inrt","split":"","minSdkVersion":"17"}}]
|
||||
@ -1,15 +1,14 @@
|
||||
package com.stardust.scriptdroid.autojs.build;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import com.stardust.autojs.apkbuilder.ApkBuilder;
|
||||
import com.stardust.autojs.apkbuilder.ManifestEditor;
|
||||
import com.stardust.autojs.apkbuilder.util.StreamUtils;
|
||||
import com.stardust.autojs.project.ProjectConfig;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.App;
|
||||
|
||||
import org.androidannotations.annotations.Click;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
@ -105,10 +104,28 @@ public class AutoJsApkBuilder extends ApkBuilder {
|
||||
}
|
||||
|
||||
public AutoJsApkBuilder setScriptFile(String path) throws IOException {
|
||||
replaceFile("assets/script.js", path);
|
||||
if (PFiles.isDir(path)) {
|
||||
copyDir("assets/project/", path);
|
||||
} else {
|
||||
replaceFile("assets/project/main.js", path);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void copyDir(String relativePath, String path) throws IOException {
|
||||
File fromDir = new File(path);
|
||||
File toDir = new File(this.mWorkspacePath, relativePath);
|
||||
toDir.mkdir();
|
||||
for (File child : fromDir.listFiles()) {
|
||||
if (child.isFile()) {
|
||||
StreamUtils.write(new FileInputStream(child),
|
||||
new FileOutputStream(new File(toDir, child.getName())));
|
||||
} else {
|
||||
copyDir(PFiles.join(relativePath, child.getName() + "/"), child.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApkBuilder replaceFile(String relativePath, String newFilePath) throws IOException {
|
||||
StreamUtils.write(new FileInputStream(newFilePath), new FileOutputStream(new File(this.mWorkspacePath, relativePath)));
|
||||
@ -123,10 +140,42 @@ public class AutoJsApkBuilder extends ApkBuilder {
|
||||
.setVersionCode(config.versionCode)
|
||||
.setPackageName(config.packageName);
|
||||
setArscPackageName(config.packageName);
|
||||
updateProjectConfig(config);
|
||||
setScriptFile(config.jsPath);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void updateProjectConfig(AppConfig config) {
|
||||
if (!PFiles.isDir(config.jsPath)) {
|
||||
return;
|
||||
}
|
||||
ProjectConfig projectConfig = ProjectConfig.fromProjectDir(config.jsPath);
|
||||
if (projectConfig == null)
|
||||
projectConfig = new ProjectConfig();
|
||||
projectConfig.setName(config.appName)
|
||||
.setPackageName(config.packageName)
|
||||
.setVersionCode(config.versionCode)
|
||||
.setVersionName(config.versionName)
|
||||
.setMainScriptFile("main.js");
|
||||
updateProjectConfigAssets(projectConfig, config.jsPath, config.jsPath);
|
||||
PFiles.write(ProjectConfig.configFileOfDir(config.jsPath), projectConfig.toJson());
|
||||
}
|
||||
|
||||
private void updateProjectConfigAssets(ProjectConfig config, String projectDir, String dir) {
|
||||
File main = new File(projectDir, config.getMainScriptFile());
|
||||
for (File file : new File(dir).listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
updateProjectConfigAssets(config, projectDir, file.getPath());
|
||||
continue;
|
||||
}
|
||||
if (file.equals(main)) {
|
||||
continue;
|
||||
}
|
||||
String relative = new File(projectDir).toURI().relativize(file.toURI()).getPath();
|
||||
config.getAssets().add(relative);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AutoJsApkBuilder build() throws IOException {
|
||||
if (mProgressCallback != null) {
|
||||
|
||||
@ -4,6 +4,7 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Environment;
|
||||
import android.support.design.widget.TextInputEditText;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.text.TextUtils;
|
||||
@ -13,6 +14,7 @@ import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.autojs.project.ProjectConfig;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.build.AutoJsApkBuilder;
|
||||
import com.stardust.scriptdroid.build.ApkBuilderPluginHelper;
|
||||
@ -102,7 +104,6 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
|
||||
}
|
||||
|
||||
private void setupWithSourceFile(ScriptFile file) {
|
||||
mSourcePath.setText(file.getPath());
|
||||
String dir = file.getParent();
|
||||
if (dir.startsWith(getFilesDir().getPath())) {
|
||||
dir = StorageFileProvider.DEFAULT_DIRECTORY_PATH;
|
||||
@ -110,6 +111,7 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
|
||||
mOutputPath.setText(dir);
|
||||
mAppName.setText(file.getSimplifiedName());
|
||||
mPackageName.setText(getString(R.string.format_default_package_name, System.currentTimeMillis()));
|
||||
setSource(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -123,12 +125,25 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
|
||||
String initialDir = new File(mSourcePath.getText().toString()).getParent();
|
||||
new FileChooserDialogBuilder(this)
|
||||
.title(R.string.text_source_file_path)
|
||||
.dir(initialDir == null ? StorageFileProvider.DEFAULT_DIRECTORY_PATH : initialDir)
|
||||
.justScriptFile()
|
||||
.singleChoice(file -> mSourcePath.setText(file.getPath()))
|
||||
.dir(Environment.getExternalStorageDirectory().getPath(),
|
||||
initialDir == null ? StorageFileProvider.DEFAULT_DIRECTORY_PATH : initialDir)
|
||||
.singleChoice(this::setSource)
|
||||
.show();
|
||||
}
|
||||
|
||||
private void setSource(File file) {
|
||||
mSourcePath.setText(file.getPath());
|
||||
if (!file.isDirectory())
|
||||
return;
|
||||
ProjectConfig config = ProjectConfig.fromProjectDir(file.getPath());
|
||||
if (config == null)
|
||||
return;
|
||||
mAppName.setText(config.getName());
|
||||
mPackageName.setText(config.getPackageName());
|
||||
mVersionCode.setText(String.valueOf(config.getVersionCode()));
|
||||
mVersionName.setText(config.getVersionName());
|
||||
}
|
||||
|
||||
|
||||
@Click(R.id.select_output)
|
||||
void selectOutputDirPath() {
|
||||
@ -221,6 +236,7 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
|
||||
mProgressDialog = new MaterialDialog.Builder(this)
|
||||
.progress(true, 100)
|
||||
.content(R.string.text_on_progress)
|
||||
.cancelable(false)
|
||||
.show();
|
||||
}
|
||||
|
||||
|
||||
@ -22,6 +22,8 @@ import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.storage.file.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.ui.build.BuildActivity;
|
||||
import com.stardust.scriptdroid.ui.build.BuildActivity_;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptLoopDialog;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
||||
import com.stardust.scriptdroid.ui.viewmodel.ScriptList;
|
||||
@ -246,6 +248,12 @@ public class ScriptListView extends ThemeColorSwipeRefreshLayout implements Swip
|
||||
.timedTask(mSelectedScriptFile);
|
||||
notifyOperated();
|
||||
break;
|
||||
case R.id.action_build_apk:
|
||||
BuildActivity_.intent(getContext())
|
||||
.extra(BuildActivity.EXTRA_SOURCE_FILE, mSelectedScriptFile.getPath())
|
||||
.start();
|
||||
notifyOperated();
|
||||
break;
|
||||
case R.id.action_sort_by_date:
|
||||
sort(ScriptList.SORT_TYPE_DATE, mDirSortMenuShowing);
|
||||
break;
|
||||
|
||||
@ -8,4 +8,8 @@
|
||||
android:id="@+id/delete"
|
||||
android:title="@string/text_delete"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_build_apk"
|
||||
android:title="@string/text_build_apk"/>
|
||||
|
||||
</menu>
|
||||
@ -33,6 +33,10 @@
|
||||
<item
|
||||
android:id="@+id/open_by_other_apps"
|
||||
android:title="@string/text_open_by_other_apps"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_build_apk"
|
||||
android:title="@string/text_build_apk"/>
|
||||
</menu>
|
||||
</item>
|
||||
|
||||
|
||||
@ -252,7 +252,7 @@
|
||||
<string name="apk_builder_build">构建中</string>
|
||||
<string name="apk_builder_package">打包中</string>
|
||||
<string name="apk_builder_clean">清理临时文件</string>
|
||||
<string name="text_source_file_path">脚本文件路径</string>
|
||||
<string name="text_source_file_path">脚本文件(夹)路径</string>
|
||||
<string name="text_search">搜索</string>
|
||||
<string name="text_select_icon">选择图标</string>
|
||||
<string name="text_use_android_n_shortcut">添加到安卓7.0 Shortcut</string>
|
||||
|
||||
@ -65,6 +65,8 @@ public interface ScriptEngine<S extends ScriptSource> {
|
||||
|
||||
@Override
|
||||
public synchronized void setTag(String key, Object value) {
|
||||
if(value == null)
|
||||
return;
|
||||
mTags.put(key, value);
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,136 @@
|
||||
package com.stardust.autojs.project;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/1/24.
|
||||
*/
|
||||
|
||||
public class ProjectConfig {
|
||||
|
||||
public static final String CONFIG_FILE_NAME = "project.json";
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
@SerializedName("name")
|
||||
private String mName;
|
||||
|
||||
@SerializedName("versionName")
|
||||
private String mVersionName;
|
||||
|
||||
@SerializedName("versionCode")
|
||||
private int mVersionCode;
|
||||
|
||||
@SerializedName("packageName")
|
||||
private String mPackageName;
|
||||
|
||||
@SerializedName("main")
|
||||
private String mMainScriptFile;
|
||||
|
||||
@SerializedName("assets")
|
||||
private List<String> mAssets = new ArrayList<>();
|
||||
|
||||
public static ProjectConfig fromJson(String json) {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
return GSON.fromJson(json, ProjectConfig.class);
|
||||
}
|
||||
|
||||
|
||||
public static ProjectConfig fromAssets(Context context, String path) {
|
||||
try {
|
||||
return fromJson(PFiles.read(context.getAssets().open(path)));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ProjectConfig fromFile(String path) {
|
||||
try {
|
||||
return fromJson(PFiles.read(path));
|
||||
} catch (UncheckedIOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ProjectConfig fromProjectDir(String path) {
|
||||
return fromFile(configFileOfDir(path));
|
||||
}
|
||||
|
||||
|
||||
public static String configFileOfDir(String projectDir) {
|
||||
return PFiles.join(projectDir, CONFIG_FILE_NAME);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return mName;
|
||||
}
|
||||
|
||||
public ProjectConfig setName(String name) {
|
||||
mName = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getVersionName() {
|
||||
return mVersionName;
|
||||
}
|
||||
|
||||
public ProjectConfig setVersionName(String versionName) {
|
||||
mVersionName = versionName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getVersionCode() {
|
||||
return mVersionCode;
|
||||
}
|
||||
|
||||
public ProjectConfig setVersionCode(int versionCode) {
|
||||
mVersionCode = versionCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return mPackageName;
|
||||
}
|
||||
|
||||
public ProjectConfig setPackageName(String packageName) {
|
||||
mPackageName = packageName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMainScriptFile() {
|
||||
return mMainScriptFile;
|
||||
}
|
||||
|
||||
public ProjectConfig setMainScriptFile(String mainScriptFile) {
|
||||
mMainScriptFile = mainScriptFile;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<String> getAssets() {
|
||||
return mAssets;
|
||||
}
|
||||
|
||||
public void setAssets(List<String> assets) {
|
||||
mAssets = assets;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return GSON.toJson(this);
|
||||
}
|
||||
|
||||
}
|
||||
@ -278,6 +278,34 @@ public class PFiles {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean copyAssetDir(Context context, String assetsDir, String toDir) {
|
||||
new File(toDir).mkdirs();
|
||||
AssetManager manager = context.getAssets();
|
||||
try {
|
||||
String[] list = manager.list(assetsDir);
|
||||
if (list == null)
|
||||
return false;
|
||||
for (String file : list) {
|
||||
InputStream stream;
|
||||
try {
|
||||
stream = manager.open(join(assetsDir, file));
|
||||
} catch (IOException e) {
|
||||
if (!copyAssetDir(context, join(assetsDir, file), join(toDir, file))) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
copyStream(stream, join(toDir, file));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public static String renameWithoutExtensionAndReturnNewPath(String path, String newName) {
|
||||
File file = new File(path);
|
||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||
@ -431,4 +459,5 @@ public class PFiles {
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
<activity android:name=".SettingsActivity"/>
|
||||
|
||||
<service
|
||||
android:name=".rt.AccessibilityService"
|
||||
android:name=".autojs.AccessibilityService"
|
||||
android:label="inrt"
|
||||
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
|
||||
<intent-filter>
|
||||
|
||||
BIN
inrt/src/main/assets/project/auto.js.png
Normal file
BIN
inrt/src/main/assets/project/auto.js.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.8 KiB |
32
inrt/src/main/assets/project/main.js
Normal file
32
inrt/src/main/assets/project/main.js
Normal file
@ -0,0 +1,32 @@
|
||||
"ui";
|
||||
|
||||
ui.layout(
|
||||
<scroll>
|
||||
<vertical bg="#707070" padding="16">
|
||||
<text text="网络图片" textColor="black" textSize="16sp" marginTop="16"/>
|
||||
<img src="file://auto.js.png"
|
||||
w="100" h="100"/>
|
||||
|
||||
<text text="带边框的图片" textColor="black" textSize="16sp" marginTop="16"/>
|
||||
<img src="http://www.autojs.org/assets/uploads/profile/1-profileavatar.jpeg"
|
||||
w="100" h="100" borderWidth="2dp" borderColor="#202020"/>
|
||||
|
||||
<text text="圆形图片" textColor="black" textSize="16sp" marginTop="16"/>
|
||||
<img src="http://www.autojs.org/assets/uploads/profile/1-profileavatar.jpeg"
|
||||
w="100" h="100" circle="true"/>
|
||||
|
||||
<text text="带边框的圆形图片" textColor="black" textSize="16sp" marginTop="16"/>
|
||||
<img src="http://www.autojs.org/assets/uploads/profile/1-profileavatar.jpeg"
|
||||
w="100" h="100" circle="true" borderWidth="2dp" borderColor="#202020"/>
|
||||
|
||||
<text text="圆角图片" textColor="black" textSize="16sp" marginTop="16"/>
|
||||
<img id="rounded_img" src="http://www.autojs.org/assets/uploads/profile/1-profileavatar.jpeg"
|
||||
w="100" h="100" radius="20dp" scaleType="fitXY"/>
|
||||
<button id="change_img" text="更改图片"/>
|
||||
</vertical>
|
||||
</scroll>
|
||||
);
|
||||
|
||||
ui.change_img.click(()=>{
|
||||
ui.rounded_img.setSource("http://www.autojs.org/assets/uploads/profile/1-profilecover.jpeg");
|
||||
});
|
||||
10
inrt/src/main/assets/project/project.json
Normal file
10
inrt/src/main/assets/project/project.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "Hello World",
|
||||
"versionName": "1.0.0",
|
||||
"versionCode": 1,
|
||||
"packageName": "org.autojs.example",
|
||||
"main": "main.js",
|
||||
"assets" : [
|
||||
"auto.js.png"
|
||||
]
|
||||
}
|
||||
@ -1,2 +0,0 @@
|
||||
auto();
|
||||
toast(files.read("/sdcard/1.txt"));
|
||||
@ -1,10 +1,9 @@
|
||||
package com.stardust.auojs.inrt;
|
||||
|
||||
import android.app.Application;
|
||||
import android.app.Fragment;
|
||||
|
||||
import com.stardust.auojs.inrt.rt.AutoJs;
|
||||
import com.stardust.auojs.inrt.rt.GlobalKeyObserver;
|
||||
import com.stardust.auojs.inrt.autojs.AutoJs;
|
||||
import com.stardust.auojs.inrt.autojs.GlobalKeyObserver;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/7/1.
|
||||
|
||||
@ -11,11 +11,10 @@ import android.support.v7.widget.Toolbar;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.stardust.auojs.inrt.rt.AutoJs;
|
||||
import com.stardust.auojs.inrt.autojs.AutoJs;
|
||||
import com.stardust.auojs.inrt.launch.AssetsProjectLauncher;
|
||||
import com.stardust.autojs.core.console.ConsoleView;
|
||||
import com.stardust.autojs.core.console.StardustConsole;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -46,15 +45,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
private void runScript() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
String js = PFiles.read(getAssets().open("script.js"));
|
||||
StringScriptSource source = new StringScriptSource("main", js);
|
||||
AutoJs.getInstance().getScriptEngineService().execute(source);
|
||||
} catch (Exception e) {
|
||||
AutoJs.getInstance().getGlobalConsole().log(e);
|
||||
}
|
||||
}).start();
|
||||
new Thread(() -> new AssetsProjectLauncher("project", this).launch()).start();
|
||||
if (!Pref.shouldShowMainActivity()) {
|
||||
finish();
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package com.stardust.auojs.inrt.rt;
|
||||
package com.stardust.auojs.inrt.autojs;
|
||||
|
||||
import android.accessibilityservice.AccessibilityServiceInfo;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package com.stardust.auojs.inrt.rt;
|
||||
package com.stardust.auojs.inrt.autojs;
|
||||
|
||||
import android.accessibilityservice.AccessibilityService;
|
||||
import android.content.Context;
|
||||
@ -1,9 +1,7 @@
|
||||
package com.stardust.auojs.inrt.rt;
|
||||
package com.stardust.auojs.inrt.autojs;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import com.stardust.auojs.inrt.App;
|
||||
import com.stardust.auojs.inrt.Pref;
|
||||
@ -1,4 +1,4 @@
|
||||
package com.stardust.auojs.inrt.rt;
|
||||
package com.stardust.auojs.inrt.autojs;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
@ -0,0 +1,55 @@
|
||||
package com.stardust.auojs.inrt.launch;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.stardust.auojs.inrt.autojs.AutoJs;
|
||||
import com.stardust.autojs.execution.ExecutionConfig;
|
||||
import com.stardust.autojs.project.ProjectConfig;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/1/24.
|
||||
*/
|
||||
|
||||
public class AssetsProjectLauncher {
|
||||
|
||||
private String mAssetsProjectDir;
|
||||
private String mProjectDir;
|
||||
private File mMainScriptFile;
|
||||
private ProjectConfig mProjectConfig;
|
||||
private Context mContext;
|
||||
|
||||
public AssetsProjectLauncher(String projectDir, Context context) {
|
||||
mAssetsProjectDir = projectDir;
|
||||
mContext = context;
|
||||
mProjectDir = new File(context.getFilesDir(), "project/").getPath();
|
||||
mProjectConfig = ProjectConfig.fromAssets(context, ProjectConfig.configFileOfDir(mAssetsProjectDir));
|
||||
mMainScriptFile = new File(mProjectDir, mProjectConfig.getMainScriptFile());
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
prepare();
|
||||
try {
|
||||
JavaScriptFileSource source = new JavaScriptFileSource(mMainScriptFile);
|
||||
AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
|
||||
.executePath(mProjectDir));
|
||||
} catch (Exception e) {
|
||||
AutoJs.getInstance().getGlobalConsole().error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void prepare() {
|
||||
if (mMainScriptFile.exists()) {
|
||||
return;
|
||||
}
|
||||
PFiles.copyAsset(mContext, PFiles.join(mAssetsProjectDir, mProjectConfig.getMainScriptFile()),
|
||||
mMainScriptFile.getPath());
|
||||
for (String asset : mProjectConfig.getAssets()) {
|
||||
PFiles.copyAsset(mContext, PFiles.join(mAssetsProjectDir, asset), PFiles.join(mProjectDir, asset));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user