Skip to content

Commit

Permalink
Merge pull request #1 from Next2D/develop
Browse files Browse the repository at this point in the history
iOS/Android/macOS/windowのエミュレータ起動に対応
  • Loading branch information
ienaga authored May 19, 2023
2 parents ccd1234 + 14c6a07 commit fcc10fb
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [ main ]
branches: [ main, develop ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [ main, develop ]
schedule:
- cron: '38 23 * * 1'

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
- develop

jobs:
macos-browser-test:
Expand Down
160 changes: 112 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ if (15 > version.split(".")[0]) {
process.exit(1);
}

const fs = require("fs");
const webpackConfig = require(`${process.cwd()}/webpack.config.js`);
const fs = require("fs");
const cp = require("child_process");
const webpackConfig = require(`${process.cwd()}/webpack.config.js`);
const electronBuilder = require("electron-builder");
const chalk = require("chalk");

let platform = "";
let environment = "";
Expand Down Expand Up @@ -119,32 +121,36 @@ const buildWeb = () =>
{
return new Promise((resolve) =>
{
const cp = require("child_process");

const command = `${process.cwd()}/node_modules/.bin/webpack`;

const mode = debug ? "development" : "production";
const stream = cp.spawn(command, [
"--mode",
"production",
mode,
"--env",
environment
]);

stream.on("error", (error) =>
let errorCheck = false;
stream.stderr.on("data", (error) =>
{
console.error(error);
process.exit(1);
errorCheck = true;
console.error(chalk.red(error.toString()));
});

stream.on("data", (data) =>
stream.stdout.on("data", (data) =>
{
console.log("koko");
console.log(data.toString());
});

stream.on("exit", () =>
{
console.log("build web app.");
if (errorCheck) {
process.exit(1);
}

console.log(chalk.green("Build web app."));
console.log();
resolve();
});
});
Expand All @@ -163,13 +169,15 @@ const generateElectronConfig = (platform) =>
let config = {};

const configPath = `${process.cwd()}/electron.build.js`;
if (fs.existsSync(configPath)) {
config = Object.assign(config, require(configPath));
if (!fs.existsSync(configPath)) {
console.error(chalk.red("The file `electron.build.js` could not be found."));
process.exit(1);
}

config = Object.assign(config, require(configPath));
if (!config.appId) {
console.log();
console.log("`appId` is not set.");
console.log(chalk.red("`appId` is not set."));
console.log("Please set `appId` in `electron.build.js`.");
console.log();
process.exit(1);
Expand Down Expand Up @@ -204,10 +212,6 @@ const generateElectronConfig = (platform) =>
break;

default:
console.log();
console.log("The specified platform could not be found.");
console.log();
process.exit(1);
break;

}
Expand Down Expand Up @@ -300,7 +304,6 @@ const buildWindow = () =>

if (debug) {

const cp = require("child_process");
const stream = cp.spawn(
`${process.cwd()}/node_modules/.bin/electron`,
[
Expand All @@ -319,7 +322,7 @@ const buildWindow = () =>
unlinkElectronIndexPath();
});

stream.on("destroy", () =>
stream.on("disconnect", () =>
{
if (generatedHTML) {
fs.unlinkSync(htmlFile);
Expand Down Expand Up @@ -387,8 +390,6 @@ const buildMac = () =>

if (debug) {

const cp = require("child_process");

const stream = cp.spawn(
`${process.cwd()}/node_modules/.bin/electron`,
[
Expand All @@ -407,7 +408,7 @@ const buildMac = () =>
unlinkElectronIndexPath();
});

stream.on("destroy", () =>
stream.on("disconnect", () =>
{
if (generatedHTML) {
fs.unlinkSync(htmlFile);
Expand Down Expand Up @@ -438,43 +439,106 @@ const buildMac = () =>
};

/**
* @description Steam用アプリの書き出し関数
* Export function for Steam apps
* @description iOSのプロジェクトを生成
* Generate iOS project
*
* @return {Promise<void>}
* @method
* @public
*/
const buildSteam = () =>
const generateNativeProject = () =>
{
buildWeb();
if (fs.existsSync(`${process.cwd()}/${platform}`)) {
return Promise.resolve();
}

return new Promise((resolve) =>
{
const stream = cp.spawn("npx", ["cap", "add", platform]);

let errorCheck = false;
stream.stderr.on("data", (error) =>
{
errorCheck = true;
console.error(chalk.red(error.toString()));
});

stream.stdout.on("data", (data) =>
{
console.log(data.toString());
});

stream.on("exit", () =>
{
if (errorCheck) {
process.exit(1);
}

console.log(chalk.green(`Successfully generated ${platform} project.`));
console.log();

resolve();
});
});
};

/**
* @description iOS用アプリの書き出し関数
* Export function for iOS apps
*
* @return {void}
* @method
* @public
*/
const buildIOS = () =>
const runNative = () =>
{
buildWeb();
generateNativeProject()
.then(() =>
{
return buildWeb();
})
.then(() =>
{
/**
* Capacitorの書き出しに必要な設定を生成
* Generate settings necessary for exporting Capacitor
*/
const config = JSON.parse(
fs.readFileSync(`${process.cwd()}/capacitor.config.json`, { "encoding": "utf8" })
);

// TODO
};
/**
* 読み込みようのindex.htmlファイルがなければ生成
* If there is no index.html file to load, generate one.
*/
const htmlFile = `${webpackConfig.output.path}/index.html`;
const generatedHTML = buildHTML(htmlFile, config.appName);

/**
* @description android用アプリの書き出し関数
* Export function for android apps
*
* @method
* @public
*/
const buildAndroid = () =>
{
buildWeb();
config.webDir = htmlFile
.replace(process.cwd(), ".")
.replace("index.html", "")
.trim();

fs.writeFileSync(
`${process.cwd()}/capacitor.config.json`,
JSON.stringify(config, null, 2)
);

return Promise.resolve(generatedHTML);

// TODO
})
.then((generated_html) =>
{
process.on("exit", () =>
{
if (generated_html) {
fs.unlinkSync(`${webpackConfig.output.path}/index.html`);
}
});

const { run } = require("@capacitor/cli");
run();
});
};

switch (platform) {
Expand All @@ -488,11 +552,15 @@ switch (platform) {
break;

case "ios":
buildIOS();
if (debug) {
runNative();
}
break;

case "android":
buildAndroid();
if (debug) {
runNative();
}
break;

case "web":
Expand All @@ -507,10 +575,6 @@ switch (platform) {
});
break;

case "steam":
buildSteam();
break;

default:
console.log();
console.log("`--platform` can be specified for macOS, Windows, iOS, Android, and Web");
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next2d/builder",
"version": "0.0.1",
"version": "0.0.2",
"description": "Multi-platform builder for Next2d Framework, supporting export to various platforms such as macOS, Windows, iOS, Android and Web(HTML)",
"author": "Toshiyuki Ienaga <ienaga@tvon.jp>",
"license": "MIT",
Expand All @@ -26,8 +26,10 @@
"@next2d/builder": "index.js"
},
"dependencies": {
"@capacitor/cli": "^5.0.3",
"@next2d/env": "^1.0.1",
"@types/node": "^20.1.0",
"chalk": "4.1.0",
"electron": "^24.2.0",
"electron-builder": "^23.6.0"
},
Expand Down

0 comments on commit fcc10fb

Please sign in to comment.