提交 048a7776 编写于 作者: fxy060608's avatar fxy060608

wip(uts): automator

上级 400112b1
"use strict";var t=require("fs"),e=require("debug"),s=require("postcss-selector-parser"),r=require("fs-extra"),i=require("licia/dateFormat"),a=require("path"),n=require("util"),o=require("licia/sleep");function l(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var c=l(t),u=l(e),d=l(s),h=l(r),p=l(i),m=l(o);function y(t){t.walk((t=>{if("tag"===t.type){const e=t.value;t.value="page"===e?"body":"uni-"+e}}))}u.default("automator:devtool");const f=["Page.getElement","Page.getElements","Element.getElement","Element.getElements"];const g=/^win/.test(process.platform);function w(t){try{return require(t)}catch(e){return require(require.resolve(t,{paths:[process.cwd()]}))}}const $=u.default("automator:launcher"),M=n.promisify(c.default.readdir),v=n.promisify(c.default.stat);async function P(t){const e=await M(t);return(await Promise.all(e.map((async e=>{const s=a.resolve(t,e);return(await v(s)).isDirectory()?P(s):s})))).reduce(((t,e)=>t.concat(e)),[])}class E{constructor(t){this.id=t.id,this.app=t.executablePath,this.appid=t.appid||"HBuilder",this.package=t.package||"io.dcloud.HBuilder"}shouldPush(){return this.exists(this.FILE_APP_SERVICE).then((()=>($(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} exists`),!1))).catch((()=>($(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} not exists`),!0)))}push(t){return P(t).then((e=>{const s=e.map((e=>{const s=(t=>g?t.replace(/\\/g,"/"):t)(a.join(this.DIR_WWW,a.relative(t,e)));return $(`${p.default("yyyy-mm-dd HH:MM:ss:l")} push ${e} ${s}`),this.pushFile(e,s)}));return Promise.all(s)})).then((t=>!0))}get FILE_APP_SERVICE(){return`${this.DIR_WWW}/app-service.js`}}const S=u.default("automator:simctl");function A(t){const e=parseInt(t);return e>9?String(e):"0"+e}class _ extends E{constructor(){super(...arguments),this.bundleVersion=""}async init(){const t=w("node-simctl").Simctl;this.tool=new t({udid:this.id});try{await this.tool.bootDevice()}catch(t){}await this.initSDCard(),S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} init ${this.id}`)}async initSDCard(){const t=await this.tool.appInfo(this.package);S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} appInfo ${t}`);const e=t.match(/DataContainer\s+=\s+"(.*)"/);if(!e)return Promise.resolve("");const s=t.match(/CFBundleVersion\s+=\s+(.*);/);if(!s)return Promise.resolve("");this.sdcard=e[1].replace("file:",""),this.bundleVersion=s[1],S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.sdcard}`)}async version(){return Promise.resolve(this.bundleVersion)}formatVersion(t){const e=t.split(".");return 3!==e.length?t:e[0]+A(e[1])+A(e[2])}async install(){return S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.app}`),await this.tool.installApp(this.app),await this.tool.grantPermission(this.package,"all"),await this.initSDCard(),Promise.resolve(!0)}async start(){try{await this.tool.terminateApp(this.package)}catch(t){}try{await this.tool.launchApp(this.package)}catch(t){console.error(t)}return Promise.resolve(!0)}async exit(){return await this.tool.terminateApp(this.package),await this.tool.shutdownDevice(),Promise.resolve(!0)}async captureScreenshot(){return Promise.resolve(await this.tool.getScreenshot())}exists(t){return h.default.existsSync(t)?Promise.resolve(!0):Promise.reject(Error(`${t} not exists`))}pushFile(t,e){return Promise.resolve(h.default.copySync(t,e))}get DIR_WWW(){return`${this.sdcard}/Documents/Pandora/apps/${this.appid}/www/`}}const H=w("adbkit"),x=u.default("automator:adb");class D extends E{async init(){if(this.tool=H.createClient(),!this.id){const t=await this.tool.listDevices();if(!t.length)throw Error("Device not found");this.id=t[0].id}this.sdcard=(await this.shell(this.COMMAND_EXTERNAL)).trim(),x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} init ${this.id} ${this.sdcard}`)}version(){return this.shell(this.COMMAND_VERSION).then((t=>{const e=t.match(/versionName=(.*)/);return e&&e.length>1?e[1]:""}))}formatVersion(t){return t}async install(){let t=!0;try{const e=(await this.tool.getProperties(this.id))["ro.build.version.release"].split(".")[0];parseInt(e)<6&&(t=!1)}catch(t){}if(x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.app} permission=${t}`),t){const t=w("adbkit/lib/adb/command.js"),e=t.prototype._send;t.prototype._send=function(t){return 0===t.indexOf("shell:pm install -r ")&&(t=t.replace("shell:pm install -r ","shell:pm install -r -g "),x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${t} `)),e.call(this,t)}}return this.tool.install(this.id,this.app).then((()=>this.init()))}start(){return this.exit().then((()=>this.shell(this.COMMAND_START)))}exit(){return this.shell(this.COMMAND_STOP)}captureScreenshot(){return this.tool.screencap(this.id).then((t=>new Promise((e=>{const s=[];t.on("data",(function(t){s.push(t)})),t.on("end",(function(){e(Buffer.concat(s).toString("base64"))}))}))))}exists(t){return this.tool.stat(this.id,t)}pushFile(t,e){return this.tool.push(this.id,t,e)}shell(t){return x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} SEND ► ${t}`),this.tool.shell(this.id,t).then(H.util.readAll).then((t=>{const e=t.toString();return x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`),e}))}get DIR_WWW(){return`/storage/emulated/0/Android/data/${this.package}/apps/${this.appid}/www`}get COMMAND_EXTERNAL(){return"echo $EXTERNAL_STORAGE"}get COMMAND_VERSION(){return`dumpsys package ${this.package}`}get COMMAND_STOP(){return`am force-stop ${this.package}`}get COMMAND_START(){return`am start -n ${this.package}/io.dcloud.PandoraEntry --es ${this.appid} --ez needUpdateApp false --ez reload true`}}const b=u.default("automator:devtool");let C,I=!1;const N={android:/android_version=(.*)/,ios:/iphone_version=(.*)/};const R={"Tool.close":{reflect:async()=>{}},"App.exit":{reflect:async()=>C.exit()},"App.enableLog":{reflect:()=>Promise.resolve()},"App.captureScreenshot":{reflect:async(t,e)=>{const s=await C.captureScreenshot(e);return b(`App.captureScreenshot ${s.length}`),{data:s}}}};!function(t){f.forEach((e=>{t[e]=function(t){return{reflect:async(e,s)=>e(t,s,!1),params:t=>(t.selector&&(t.selector=d.default(y).processSync(t.selector)),t)}}(e)}))}(R);const O={devtools:{name:"App",paths:[],required:["manifest.json","app-service.js"],validate:async function(t,e){t.platform=(t.platform||process.env.UNI_OS_NAME).toLocaleLowerCase(),Object.assign(t,t[t.platform]),C=function(t,e){return"ios"===t?new _(e):new D(e)}(t.platform,t),await C.init();const s=await C.version();if(s){if(t.version){const e=C.formatVersion(function(t,e){if(t.endsWith(".txt"))try{const s=c.default.readFileSync(t).toString().match(N[e]);if(s)return s[1]}catch(t){console.error(t)}return t}(t.version,t.platform));b(`version: ${s}`),b(`newVersion: ${e}`),e!==s&&(I=!0)}}else I=!0;if(I){if(!t.executablePath)throw Error(`app-plus->${t.platform}->executablePath is not provided`);if(!c.default.existsSync(t.executablePath))throw Error(`${t.executablePath} not exists`)}return t},create:async function(t,e,s){I&&await C.install(),(I||s.compiled||await C.shouldPush())&&(await C.push(t),await m.default(1e3)),await C.start()}},adapter:R};module.exports=O;
"use strict";var t=require("fs"),e=require("debug"),s=require("postcss-selector-parser"),i=require("fs-extra"),r=require("licia/dateFormat"),a=require("path"),n=require("util"),o=require("licia/sleep");function l(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var c=l(t),u=l(e),d=l(s),h=l(i),p=l(r),m=l(o);function y(t){t.walk((t=>{if("tag"===t.type){const e=t.value;t.value="page"===e?"body":"uni-"+e}}))}u.default("automator:devtool");const f=["Page.getElement","Page.getElements","Element.getElement","Element.getElements"];const $=/^win/.test(process.platform),P=t=>$?t.replace(/\\/g,"/"):t;function E(t){try{return require(t)}catch(e){return require(require.resolve(t,{paths:[process.cwd()]}))}}const g=u.default("automator:launcher"),w=n.promisify(c.default.readdir),M=n.promisify(c.default.stat);async function v(t){const e=await w(t);return(await Promise.all(e.map((async e=>{const s=a.resolve(t,e);return(await M(s)).isDirectory()?v(s):s})))).reduce(((t,e)=>t.concat(e)),[])}class _{constructor(t){this.id=t.id,this.app=t.executablePath,this.appid=t.appid||"HBuilder",this.package=t.package||"io.dcloud.HBuilder"}shouldPush(){return this.exists(this.FILE_APP_SERVICE).then((()=>(g(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} exists`),!1))).catch((()=>(g(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} not exists`),!0)))}push(t){return v(t).then((e=>{const s=e.map((e=>{const s=P(a.join(this.DIR_WWW,a.relative(t,e)));return g(`${p.default("yyyy-mm-dd HH:MM:ss:l")} push ${e} ${s}`),this.pushFile(e,s)}));return Promise.all(s)})).then((t=>!0))}get FILE_APP_SERVICE(){return`${this.DIR_WWW}/app-service.js`}}const S=u.default("automator:simctl");function A(t){const e=parseInt(t);return e>9?String(e):"0"+e}class H extends _{constructor(){super(...arguments),this.bundleVersion=""}async init(){const t=E("node-simctl").Simctl;this.tool=new t({udid:this.id});try{await this.tool.bootDevice()}catch(t){}await this.initSDCard(),S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} init ${this.id}`)}async initSDCard(){const t=await this.tool.appInfo(this.package);S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} appInfo ${t}`);const e=t.match(/DataContainer\s+=\s+"(.*)"/);if(!e)return Promise.resolve("");const s=t.match(/CFBundleVersion\s+=\s+(.*);/);if(!s)return Promise.resolve("");this.sdcard=e[1].replace("file:",""),this.bundleVersion=s[1],S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.sdcard}`)}async version(){return Promise.resolve(this.bundleVersion)}formatVersion(t){const e=t.split(".");return 3!==e.length?t:e[0]+A(e[1])+A(e[2])}async install(){return S(`${p.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.app}`),await this.tool.installApp(this.app),await this.tool.grantPermission(this.package,"all"),await this.initSDCard(),Promise.resolve(!0)}async start(){try{await this.tool.terminateApp(this.package)}catch(t){}try{await this.tool.launchApp(this.package)}catch(t){console.error(t)}return Promise.resolve(!0)}async exit(){return await this.tool.terminateApp(this.package),await this.tool.shutdownDevice(),Promise.resolve(!0)}async captureScreenshot(){return Promise.resolve(await this.tool.getScreenshot())}exists(t){return h.default.existsSync(t)?Promise.resolve(!0):Promise.reject(Error(`${t} not exists`))}pushFile(t,e){return Promise.resolve(h.default.copySync(t,e))}get DIR_WWW(){return`${this.sdcard}/Documents/Pandora/apps/${this.appid}/www/`}}const x=u.default("automator:launcher"),I=n.promisify(c.default.readdir),D=n.promisify(c.default.stat);async function C(t){const e=await I(t);return(await Promise.all(e.map((async e=>{const s=a.resolve(t,e);return(await D(s)).isDirectory()?C(s):s})))).reduce(((t,e)=>t.concat(e)),[])}class R{constructor(t){this.id=t.id,this.app=t.executablePath,this.appid=t.appid||"HBuilder",this.package=t.package||"io.dcloud.HBuilder"}shouldPush(){return this.exists(this.FILE_APP_SERVICE).then((()=>(x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} exists`),!1))).catch((()=>(x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} not exists`),!0)))}push(t){return C(t).then((e=>{const s=e.map((e=>{const s=P(a.join(this.DIR_WWW,a.relative(t,e)));return x(`${p.default("yyyy-mm-dd HH:MM:ss:l")} push ${e} ${s}`),this.pushFile(e,s)}));return Promise.all(s)})).then((t=>!0))}get FILE_APP_SERVICE(){return`${this.DIR_WWW}/app-service.js`}}const b=E("adbkit"),V=u.default("automator:adb");class W extends R{async init(){if(this.tool=b.createClient(),!this.id){const t=await this.tool.listDevices();if(!t.length)throw Error("Device not found");this.id=t[0].id}this.sdcard=(await this.shell(this.COMMAND_EXTERNAL)).trim(),V(`${p.default("yyyy-mm-dd HH:MM:ss:l")} init ${this.id} ${this.sdcard}`)}version(){return this.shell(this.COMMAND_VERSION).then((t=>{const e=t.match(/versionName=(.*)/);return e&&e.length>1?e[1]:""}))}formatVersion(t){return t}async install(){let t=!0;try{const e=(await this.tool.getProperties(this.id))["ro.build.version.release"].split(".")[0];parseInt(e)<6&&(t=!1)}catch(t){}if(V(`${p.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.app} permission=${t}`),t){const t=E("adbkit/lib/adb/command.js"),e=t.prototype._send;t.prototype._send=function(t){return 0===t.indexOf("shell:pm install -r ")&&(t=t.replace("shell:pm install -r ","shell:pm install -r -g "),V(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ${t} `)),e.call(this,t)}}return this.tool.install(this.id,this.app).then((()=>this.init()))}start(){return this.exit().then((()=>this.shell(this.COMMAND_START)))}exit(){return this.shell(this.COMMAND_STOP)}captureScreenshot(){return this.tool.screencap(this.id).then((t=>new Promise((e=>{const s=[];t.on("data",(function(t){s.push(t)})),t.on("end",(function(){e(Buffer.concat(s).toString("base64"))}))}))))}exists(t){return this.tool.stat(this.id,t)}pushFile(t,e){return this.tool.push(this.id,t,e)}shell(t){return V(`${p.default("yyyy-mm-dd HH:MM:ss:l")} SEND ► ${t}`),this.tool.shell(this.id,t).then(b.util.readAll).then((t=>{const e=t.toString();return V(`${p.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`),e}))}get DIR_WWW(){return`/storage/emulated/0/Android/data/${this.package}/apps/${this.appid}/www`}get COMMAND_EXTERNAL(){return"echo $EXTERNAL_STORAGE"}get COMMAND_VERSION(){return`dumpsys package ${this.package}`}get COMMAND_STOP(){return`am force-stop ${this.package}`}get COMMAND_START(){return`am start -n ${this.package}/io.dcloud.PandoraEntry --es ${this.appid} --ez needUpdateApp false --ez reload true`}}const k=u.default("automator:devtool");let N,O=!1;const F={android:/android_version=(.*)/,ios:/iphone_version=(.*)/};const L={"Tool.close":{reflect:async()=>{}},"App.exit":{reflect:async()=>N.exit()},"App.enableLog":{reflect:()=>Promise.resolve()},"App.captureScreenshot":{reflect:async(t,e)=>{const s=await N.captureScreenshot(e);return k(`App.captureScreenshot ${s.length}`),{data:s}}}};!function(t){f.forEach((e=>{t[e]=function(t){return{reflect:async(e,s)=>e(t,s,!1),params:t=>(t.selector&&(t.selector=d.default(y).processSync(t.selector)),t)}}(e)}))}(L);const q={devtools:{name:"App",paths:[],required:["manifest.json","app-service.js"],validate:async function(t,e){t.platform=(t.platform||process.env.UNI_OS_NAME).toLocaleLowerCase(),Object.assign(t,t[t.platform]),N=function(t,e){return"ios"===t?new H(e):new W(e)}(t.platform,t),await N.init();const s=await N.version();if(s){if(t.version){const e=N.formatVersion(function(t,e){if(t.endsWith(".txt"))try{const s=c.default.readFileSync(t).toString().match(F[e]);if(s)return s[1]}catch(t){console.error(t)}return t}(t.version,t.platform));k(`version: ${s}`),k(`newVersion: ${e}`),e!==s&&(O=!0)}}else O=!0;if(O){if(!t.executablePath)throw Error(`app-plus->${t.platform}->executablePath is not provided`);if(!c.default.existsSync(t.executablePath))throw Error(`${t.executablePath} not exists`)}return t},create:async function(t,e,s){O&&await N.install(),(O||s.compiled||await N.shouldPush())&&(await N.push(t),await m.default(1e3)),await N.start()}},adapter:L};module.exports=q;
......@@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs-extra'
import {
emptyDir,
normalizeNodeModules,
normalizePath,
parseManifestJsonOnce,
parseVueRequest,
......@@ -106,13 +107,16 @@ export function uniAppUTSPlugin(): Plugin {
return
}
// 仅处理 uts 文件
const isMainUTS = normalizePath(id) === mainUTS
const fileName = path.relative(inputDir, id)
this.emitFile({
type: 'asset',
fileName: normalizeFilename(fileName, isMainUTS),
source: normalizeCode(code, isMainUTS),
})
// 忽略 uni-app-uts/lib/automator/index.uts
if (!filename.includes('uni-app-uts')) {
const isMainUTS = normalizePath(id) === mainUTS
const fileName = path.relative(inputDir, id)
this.emitFile({
type: 'asset',
fileName: normalizeFilename(fileName, isMainUTS),
source: normalizeCode(code, isMainUTS),
})
}
code = await parseImports(code)
return code
},
......@@ -149,7 +153,7 @@ function normalizeFilename(filename: string, isMain = false) {
if (isMain) {
return 'index.uts'
}
return filename
return normalizeNodeModules(filename)
}
function normalizeCode(code: string, isMain = false) {
......
......@@ -31,7 +31,7 @@ exports.default = [
if (platform === 'app' && process.env.UNI_APP_X === 'true') {
const automatorPath = (0, uni_cli_shared_1.normalizePath)((0, uni_cli_shared_1.resolveBuiltIn)(`@dcloudio/uni-app-uts/lib/automator/index.uts`));
return {
code: code + `;import { initAutomator } = '${automatorPath}';`,
code: code + `;import { initAutomator } from '${automatorPath}';`,
map: null,
};
}
......
......@@ -23,7 +23,8 @@
"app",
"h5",
"mp-weixin"
]
],
"uvue": true
},
"gitHead": "33e807d66e1fe47e2ee08ad9c59247e37b8884da",
"dependencies": {
......
......@@ -42,7 +42,7 @@ export default [
resolveBuiltIn(`@dcloudio/uni-app-uts/lib/automator/index.uts`)
)
return {
code: code + `;import { initAutomator } = '${automatorPath}';`,
code: code + `;import { initAutomator } from '${automatorPath}';`,
map: null,
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册