1. 程式人生 > >Cordova - Windows版本圖形介面管理工具,告別命令列輸入方式!

Cordova - Windows版本圖形介面管理工具,告別命令列輸入方式!

原文: Cordova - Windows版本圖形介面管理工具,告別命令列輸入方式!

Cordova本身提供的是命令列管理工具,並沒有提供圖形介面管理工具,雖然命令列管理工具可以完成所有Cordova管理,但是對於我這種懶蛋,可真不希望每次都輸入命令,而且我更擔心一旦輸錯一個字元,命令執行失敗,再次重新輸入,反覆執行這種無意義的事情!於是,我就用Delphi編寫了這個Windows下的管理工具,用於執行大多數Cordova命令,能夠滿足我開發Cordova的需求!

由於目前工作緊張,所以暫時未能提供教程,近期完成專案後,會補上詳細使用教程!

全部原始碼和編譯好的工具下載:https://download.csdn.net/download/sunylat/10782003

此工具目前還有很少一點點功能需要完善,但不影響正常使用,只是錦上添花!

功能描述:

1,Cordova環境檢查:對Cordova依賴的各種工具進行檢查,看是否滿足Cordova工作需要。

2,Cordova工程管理:新建、刪除、加入已建立工程、在任意工程之間切換。

3,Cordova平臺管理:新增、刪除Cordova平臺,Windows版,支援Android、IOS、Windows平臺,其餘平臺未在本工具中加入,其實可以隨時加入!

4,Cordova外掛管理:新增、刪除所有Cordova官方外掛;支援第三方外掛安裝、刪除。

5,編譯、釋出:支援Android平臺APP的編譯、釋出,可以釋出到模擬器或真機。

工具截圖:

1,

2,

3,

4,

5,

全部原始碼:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.ComCtrls, Vcl.Buttons, System.Generics.collections;

type TForm2 = class(TForm) Panel1: TPanel; Panel2: TPanel; clearLogBtn: TButton; logMemo: TMemo; Splitter1: TSplitter; Panel3: TPanel; PageControl1: TPageControl; TabSheet1: TTabSheet; Panel4: TPanel; projectPathEdt: TEdit; Label1: TLabel; TabSheet2: TTabSheet; TabSheet3: TTabSheet; TabSheet4: TTabSheet; TabSheet5: TTabSheet; GroupBox1: TGroupBox; Label2: TLabel; projectNameEdt: TEdit; Label3: TLabel; projectIdEdt: TEdit; openProjectPathBtn: TButton; GroupBox2: TGroupBox; androidCBox: TCheckBox; IOSCBox: TCheckBox; addPlatformBtn: TButton; removePlatformBtn: TButton; windowsCBox: TCheckBox; getPlatformBtn: TButton; installPluginBtn: TButton; Button5: TButton; Button6: TButton; GroupBox3: TGroupBox; checkToolRGroup: TRadioGroup; installCordovaBtn: TButton; checkToolBtn: TButton; GroupBox4: TGroupBox; checkJavaHomeBtn: TButton; setJavaHomeBtn: TButton; GroupBox5: TGroupBox; checkAndroidHomeBtn: TButton; setAndroidHomeBtn: TButton; GroupBox6: TGroupBox; batteryStatusCBox: TCheckBox; cameraCBox: TCheckBox; contactsCBox: TCheckBox; deviceCBox: TCheckBox; deviceMotionCBox: TCheckBox; compassCBox: TCheckBox; dialogsCBox: TCheckBox; fileCBox: TCheckBox; fileTransferCBox: TCheckBox; geolocationCBox: TCheckBox; globalizationCBox: TCheckBox; mediaCBox: TCheckBox; mediaCaptureCBox: TCheckBox; networkCBox: TCheckBox; splashScreenCBox: TCheckBox; vibrationCBox: TCheckBox; statusbarCBox: TCheckBox; consoleCBox: TCheckBox; inappbrowserCBox: TCheckBox; selectPluginBitBtn: TBitBtn; GroupBox7: TGroupBox; downloadGradleBtn: TButton; configGradleBtn: TButton; Label7: TLabel; OpenDialog1: TOpenDialog; GroupBox8: TGroupBox; Label8: TLabel; pluginNameEdt: TEdit; selectLocalPluginBtn: TButton; isInstallThirdCBox: TCheckBox; GroupBox9: TGroupBox; buildBtn: TButton; GroupBox10: TGroupBox; sendToEmulatorBtn: TButton; Label10: TLabel; GroupBox11: TGroupBox; Label11: TLabel; sendToDeviceBtn: TButton; Label12: TLabel; isRebuildCBox: TCheckBox; selectPlatformBitBtn: TBitBtn; delPluginInfoLab: TLabel; pluginInfoLab: TLabel; projectListBox: TListBox; deleteProjectBtn: TButton; addOldProjectBtn: TButton; getProjectInfoBtn: TButton; Button8: TButton; createProjectBtn: TButton; curProjectNameEdt: TEdit; Label4: TLabel; openAndroidBuildPathBtn: TButton; openGradleDirBtn: TButton; chcpRGroup: TRadioGroup; doChcpBtn: TButton; buildPlatformRGroup: TRadioGroup; buildTypeRGroup: TRadioGroup; procedure clearLogBtnClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure createProjectBtnClick(Sender: TObject); procedure openProjectPathBtnClick(Sender: TObject); procedure checkToolBtnClick(Sender: TObject); procedure installCordovaBtnClick(Sender: TObject); procedure getProjectInfoBtnClick(Sender: TObject); procedure getPlatformBtnClick(Sender: TObject); procedure addPlatformBtnClick(Sender: TObject); procedure removePlatformBtnClick(Sender: TObject); procedure installPluginBtnClick(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure buildBtnClick(Sender: TObject); procedure Button8Click(Sender: TObject); procedure checkJavaHomeBtnClick(Sender: TObject); procedure checkAndroidHomeBtnClick(Sender: TObject); procedure selectPluginBitBtnClick(Sender: TObject); procedure downloadGradleBtnClick(Sender: TObject); procedure configGradleBtnClick(Sender: TObject); procedure sendToEmulatorBtnClick(Sender: TObject); procedure sendToDeviceBtnClick(Sender: TObject); procedure Label12Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure selectPlatformBitBtnClick(Sender: TObject); procedure TabSheet4Show(Sender: TObject); procedure deleteProjectBtnClick(Sender: TObject); procedure addOldProjectBtnClick(Sender: TObject); procedure projectListBoxDblClick(Sender: TObject); procedure selectLocalPluginBtnClick(Sender: TObject); procedure openAndroidBuildPathBtnClick(Sender: TObject); procedure openGradleDirBtnClick(Sender: TObject); procedure doChcpBtnClick(Sender: TObject); private { Private declarations } // 是否程式正在執行 function isExeRunning(exeName: string): boolean; // 終止程序 procedure KillProc(procname: string); // 得到特殊目錄 function GetSpecialDir(SpecialDirID: Integer): string; // 得到使用者目錄 function GetAppDir: string; public { Public declarations } procedure MyLog(tempLog: string); // log方法 // 關閉DOS視窗 procedure KillDosWindow; // 執行單條DOS命令 procedure RunDosCommand(commandStr: string; isPause: boolean); Overload; { // 同時執行多條DOS命令 stringList:包含要執行DOS命令的TStringList。 isPause:是否執行完畢暫停當前視窗,不關閉 } procedure RunDosCommand(var stringList: TStringList; isPause: boolean); Overload; // 判斷是否Cordova工程目錄 function IsCordovaDir(projectDir: string): boolean; // 進入Cordova工程目錄的DOS命令字串 function CdCordovaDirCommandStr(CordovaDir: string): string; { 得到選中的外掛 isAdd:是否新增外掛操作(tru,新增;false,刪除) } function GetSelectedPlugin(isAdd: boolean): string; // 選擇外掛 procedure SelectPlugin(isSelectAll: boolean); { 得到選中的平臺 isAdd:是否新增外掛平臺(tru,新增;false,刪除) } function GetSelectedPlatform: string; // 選擇平臺 procedure SelectPlatform(isSelectAll: boolean); // 顯示被管理工程資訊 procedure ShowProjectInfo(var projectDic: TDictionary<string, string>; var projectListBox: TListBox); // 通過工程全路徑名稱得到工程路徑和名稱 procedure GetProjectPathAndName(projectDir: string; var projectPath: string; var projectName: string); // 對ini檔案內容操作(新增和刪除) procedure OperateIniFile(iniFileName, section, key, value: string; isAdd: boolean); // 同步資料 procedure SyncProjectes(var projectDic: TDictionary<string, string>; var projectListBox: TListBox; iniFileName: string; isDicToIni: boolean); // 延時方法 procedure Delay(msecs: Integer); end; const { Gradle工具在使用者目錄中存放的路徑名稱 注意:當前cordova版本:8.0.0,如果cordova版本不同,需要注意正在使用的cordova存放Gradle工具目錄還是不是這個名稱, 如果不是,則直接把下面常量修改成正在使用的Gradle存放目錄名稱就可以了 我當前版本cordova存放Gradle全路徑: C:\Users\sunylat\.gradle\wrapper\dists\gradle-4.1-all\bzyivzo6n839fup2jbap0tjew } GradleDirInUserHome : string = '.gradle\wrapper\dists\gradle-4.1-all\bzyivzo6n839fup2jbap0tjew'; // cordova命令使用的常量 Cordova: String = 'cordova '; // ini配置檔名稱 configFile: string = 'config.ini'; // 預設的工程ID defaultProjectID: string = 'io.cordova.hellocordova'; // 預設的工程名稱 defaultProjectName: string = 'HelloCordova '; // DOS連線命令符合(前面命令必須成功執行,才能執行後面命令) DosJoinCode: string = '&'; // DOS空白行命令 DosBlankLine: string = 'echo.'; var Form2: TForm2; logInfo: string; // log資訊 // ----------------------------------------- curProjectPath: string; // 當前工程路徑(不包括cordova工程目錄) curProjectName: string; // 當前cordova工程名稱 curProjectDir: string; // cordova工程全路徑名稱(curProjectPath + curProjectName) DosCommand: string; // Dos命令 iniFileName: string; // ini檔案 isSelectPlugin: boolean; // 是否選中外掛 isSelectPlatfom: boolean; // 是否選中平臺 // 被管理全部工程的Dictionary projectDictionary: TDictionary<string, string>; implementation {$R *.dfm} uses FileCtrl, System.IOUtils, inifiles, ShellAPI, TLHelp32, ShlObj, System.StrUtils; procedure TForm2.addOldProjectBtnClick(Sender: TObject); var tempStr: string; tempProjectPath, tempProjectName: string; // 臨時用的工程路徑和工程名稱 begin // 選擇工程目錄 if SelectDirectory('請選擇已有工程的目錄!', '', tempStr) = true then begin // 通過選擇的工程全路徑名稱得到工程路徑和工程名稱 self.GetProjectPathAndName(tempStr, tempProjectPath, tempProjectName); // 把選中的工程資訊加入到ni檔案中 self.OperateIniFile(iniFileName, 'projectes', tempProjectName, tempProjectPath, true); // 同步資料 self.SyncProjectes(projectDictionary, projectListBox, iniFileName, false); end; end; procedure TForm2.addPlatformBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; platformStr: string; // 要新增平臺命令字串 begin // 得到選中平臺 platformStr := trim(self.GetSelectedPlatform); if platformStr <> '' then begin try tempList := TStringList.Create; // 加入進入工程目錄的DOS命令 tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); // 加入新增平臺的cordova命令 tempCommand := 'cordova platform add ' + platformStr + ' --save'; tempList.Add(tempCommand); // 執行所有命令 self.RunDosCommand(tempList, true); finally tempList.Free; end; end else begin showmessage('請選擇要新增的平臺,隨後執行新增平臺操作!'); end; end; procedure TForm2.checkJavaHomeBtnClick(Sender: TObject); begin // 檢視JAVA_HOME環境變數 DosCommand := 'echo %JAVA_HOME%'; // 執行DOS命令 self.RunDosCommand(DosCommand, true); end; procedure TForm2.checkAndroidHomeBtnClick(Sender: TObject); begin // 檢視ANDROID_HOME環境變數 DosCommand := 'echo %ANDROID_HOME%'; // 執行DOS命令 self.RunDosCommand(DosCommand, true); end; procedure TForm2.openAndroidBuildPathBtnClick(Sender: TObject); const // android debug程式編譯輸出目錄 androidDebugOutputPath = 'platforms\android\app\build\outputs\apk\debug'; // windows phone debug程式編譯輸出目錄 windowsDebugOutputPath = 'platforms\windows\build\windows\debug\anycpu'; var projectDir, openDir: string; begin projectDir := trim(projectPathEdt.Text); if projectDir <> '' then begin if buildPlatformRGroup.ItemIndex = 0 then begin openDir := projectDir + '\' + androidDebugOutputPath; end else if buildPlatformRGroup.ItemIndex = 1 then begin openDir := projectDir + '\' + windowsDebugOutputPath; end; ShellExecute(Handle, 'open', 'Explorer.exe', PChar(openDir), nil, SW_SHOWNORMAL); end; end; procedure TForm2.openProjectPathBtnClick(Sender: TObject); begin if (trim(curProjectPath) <> '') and (trim(curProjectName) <> '') then begin ShellExecute(Handle, 'open', 'Explorer.exe', PChar(curProjectDir), nil, SW_SHOWNORMAL); end else begin showmessage('請確認是否已經建立過工程?'); end; end; procedure TForm2.createProjectBtnClick(Sender: TObject); var projectID: string; // 工程ID tempIniFile: TInifile; // ini配置檔案 // 建立工程 procedure CreateProject; begin DosCommand := 'cordova create '; { A,執行新增工程命令 } // 1,在執行命令中加入工程路徑 DosCommand := DosCommand + curProjectPath + '\' + curProjectName + ' '; // 工程ID projectID := trim(projectIdEdt.Text); if projectID = '' then begin projectID := defaultProjectID; end; // 2,在執行命令中加入工程ID DosCommand := DosCommand + projectID + ' '; // 3,在執行命令中加入工程名稱 DosCommand := DosCommand + curProjectName; // 執行建立工程命令 self.RunDosCommand(DosCommand, true); { B,把剛建立工程寫入ini檔案 } try tempIniFile := TInifile.Create(iniFileName); // 當前工程名稱 tempIniFile.WriteString('curProject', 'name', curProjectName); // 當前工程目錄(不包括cordova工程目錄的全路徑名稱) tempIniFile.WriteString('curProject', 'path', curProjectPath); finally tempIniFile.Free; end; { C,把剛建立工程加入工程管理Dictionary中 } projectDictionary.Add(curProjectName, curProjectPath); { D,重新顯示全部工程資訊 } self.ShowProjectInfo(projectDictionary, projectListBox); end; begin // 選擇工程目錄 if SelectDirectory('請選擇儲存當前工程的上級資料夾,系統自動建立工程資料夾!', '', curProjectPath) then begin // 工程名稱 if trim(projectNameEdt.Text) <> '' then begin curProjectName := trim(projectNameEdt.Text); end else begin curProjectName := defaultProjectName; end; if rightStr(curProjectPath, 1) = '\' then begin // 工程全路徑名稱 curProjectDir := curProjectPath + curProjectName; end else begin // 工程全路徑名稱 curProjectDir := curProjectPath + '\' + curProjectName; end; if TDirectory.Exists(curProjectDir) = true then begin // 2,建立cordova工程 if TDirectory.IsEmpty(curProjectDir) = false then begin if MessageBox(0, '執行建立工程操作,如果當前選中工程存放目錄已經存在同名工程名稱資料夾,則此資料夾將被徹底刪除,確定建立嗎?', '提醒', MB_YESNO + MB_ICONWARNING) = IDYES then begin // 刪除已有資料夾(包括所有子資料夾和檔案) TDirectory.Delete(curProjectDir, true); self.Delay(1500); // 重新建立工程目錄 TDirectory.CreateDirectory(curProjectDir); // 執行建立工程 CreateProject; end; end else begin // 建立工程目錄 TDirectory.CreateDirectory(curProjectDir); // 執行建立工程 CreateProject; end; end else begin // 建立工程目錄 TDirectory.CreateDirectory(curProjectDir); // 執行建立工程 CreateProject; end; // 顯示當前工程名稱 curProjectNameEdt.Text := curProjectName; // 顯示建立工程的全路徑名稱 projectPathEdt.Text := curProjectDir; // 同步工程資訊 SyncProjectes(projectDictionary, projectListBox, iniFileName, true); end; end; procedure TForm2.sendToEmulatorBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; runCommand: string; // 執行安卓程式命令 begin try tempList := TStringList.Create; tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); // 組裝把安卓程式傳送到模擬器執行的命令 if isRebuildCBox.Checked = true then begin runCommand := 'cordova run android --emulator'; end else begin runCommand := 'cordova run android --nobuild --emulator'; end; // 加入把安卓程式傳送到模擬器執行的命令 tempList.Add(runCommand); self.RunDosCommand(tempList, true); finally tempList.Free; // 視窗最小化到工作列 Application.Minimize; end; end; procedure TForm2.TabSheet4Show(Sender: TObject); var tempStr: string; begin { 1,第三方外掛安裝方法 } tempStr := '第三方外掛安裝方法:必須選中"安裝第三方外掛"複選框後,採用下面兩種方式安裝。' + #13; tempStr := tempStr + '1,遠端下載:在"外掛名稱或URL"文字框輸入下載外掛的url。' + #13; tempStr := tempStr + '2,本地安裝:點選"選擇本地外掛"按鈕,選擇本地外掛。'; // 顯示設定第三方外掛安裝方法 pluginInfoLab.Caption := tempStr; { 2,第三方外掛刪除方法 } tempStr := '第三方外掛刪除方法:在"外掛名稱或URL"文字框輸入刪除外掛名稱。' + #13; tempStr := tempStr + '注意:刪除的外掛名稱和安裝時候的名稱不一樣!' + #13; tempStr := tempStr + '刪除的外掛名稱是安裝時候的名稱的最後一部分,' + #13; tempStr := tempStr + '是去掉整個名稱的"cordova-plugin-"之後剩餘的字串!,' + #13; tempStr := tempStr + '例如:camera外掛' + #13; tempStr := tempStr + '安裝名稱:cordova-plugin-camera' + #13; tempStr := tempStr + '刪除名稱:camera'; delPluginInfoLab.Caption := tempStr; end; procedure TForm2.sendToDeviceBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; runCommand: string; // 執行安卓程式命令 begin try tempList := TStringList.Create; tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); // 組裝把安卓程式傳送到模擬器執行的命令 if isRebuildCBox.Checked = true then begin runCommand := 'cordova run android --device'; end else begin runCommand := 'cordova run android --nobuild --device'; end; // 加入把安卓程式傳送到模擬器執行的命令 tempList.Add(runCommand); self.RunDosCommand(tempList, true); finally tempList.Free; // 視窗最小化到工作列 Application.Minimize; end; end; procedure TForm2.installPluginBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; pluginStr: string; // 外掛列表 { 刪除攝像頭外掛配置檔案,如果不刪除,則再次安裝時候報錯,這可能是一個BUG } procedure DeleteCameraXml(tempProjectDir: string); const // 攝像頭外掛的配置檔案 cameraXml: string = 'platforms\android\app\src\main\res\xml\camera_provider_paths.xml'; var tempFile: string; // 臨時變數 begin // 攝像頭外掛的配置檔案的全路徑名稱 tempFile := tempProjectDir + '\' + cameraXml; if TFile.Exists(tempFile) = true then begin TFile.Delete(tempFile); end; end; begin // 安裝第三方外掛 if isInstallThirdCBox.Checked = true then begin // 得到要安裝外掛名稱或者url pluginStr := trim(pluginNameEdt.Text); // 可以安裝,則直接安裝 if pluginStr = '' then begin showmessage('要安裝的第三方外掛名稱或者Url不能為空!'); pluginNameEdt.SetFocus; end; end else // 安裝Cordova官方外掛 begin // 得到要安裝的外掛列表 pluginStr := self.GetSelectedPlugin(true); end; // 如果有要安裝的外掛,則執行安裝 if trim(pluginStr) <> '' then begin // 1,刪除攝像頭外掛配置檔案 DeleteCameraXml(curProjectDir); // 2,安裝指定的外掛 try tempList := TStringList.Create; // 1,加入進入Cordova工程目錄命令 tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); // 2,加入執行安裝外掛命令 tempCommand := 'cordova plugin add ' + pluginStr + ' --save --searchpath ../plugins'; tempList.Add(tempCommand); // 3,執行安裝外掛命令 self.RunDosCommand(tempList, true); finally tempList.Free; end; end else begin showmessage('請選擇要安裝的外掛後,在執行安裝外掛操作!'); end; end; procedure TForm2.openGradleDirBtnClick(Sender: TObject); var GradleSaveDir: string; // Gradle最終保持目錄名稱 userHome: string; // 使用者目錄 begin // 得到使用者目錄 userHome := StringReplace(self.GetAppDir, 'AppData\Roaming', '', [rfReplaceAll]); // Gradle最終保持目錄名稱 GradleSaveDir := userHome + GradleDirInUserHome; ShellExecute(Handle, 'open', 'Explorer.exe', PChar(GradleSaveDir), nil, SW_SHOWNORMAL); end; procedure TForm2.Button5Click(Sender: TObject); var tempList: TStringList; tempCommand: string; pluginStr: string; // 外掛列表 begin // 安裝Cordova官方外掛 if isInstallThirdCBox.Checked = false then begin // 得到要刪除的外掛列表 pluginStr := self.GetSelectedPlugin(false); end else // 安裝第三方外掛 begin // 得到要安裝外掛名稱或者url pluginStr := trim(pluginNameEdt.Text); // 可以安裝,則直接安裝 if pluginStr = '' then begin showmessage('要安裝的第三方外掛名稱或者Url不能為空!'); pluginNameEdt.SetFocus; end; end; // 執行刪除外掛命令 if trim(pluginStr) <> '' then begin try tempList := TStringList.Create; // 1,加入進入Cordova工程目錄命令 tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); // 2,加入執行刪除外掛命令 tempCommand := 'cordova plugin rm ' + pluginStr + ' --save'; tempList.Add(tempCommand); // 3,執行刪除外掛命令 self.RunDosCommand(tempList, true); finally tempList.Free; end; end else begin showmessage('請選擇要刪除的外掛後,在執行刪除外掛操作!'); end; end; procedure TForm2.Button6Click(Sender: TObject); var tempList: TStringList; tempCommand: string; begin try tempList := TStringList.Create; tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); tempList.Add('cordova plugin ls'); self.RunDosCommand(tempList, true); finally tempList.Free; end; end; procedure TForm2.buildBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; begin try tempList := TStringList.Create; // 1, 加入進入當前工程目錄命令 tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); if buildPlatformRGroup.ItemIndex = 0 then begin // 2,加入編譯cordova在安卓平臺的程式 if buildTypeRGroup.ItemIndex = 0 then begin tempList.Add('cordova build android --debug'); end else if buildTypeRGroup.ItemIndex = 1 then begin // tempList.Add('cordova build android --release -- --keystore="..\android.keystore" --storePassword=android --alias=mykey'); end; end else if buildPlatformRGroup.ItemIndex = 1 then begin // 2,加入編譯cordova在windows平臺的程式 if buildTypeRGroup.ItemIndex = 0 then begin tempList.Add('cordova build windows --debug'); end else if buildTypeRGroup.ItemIndex = 1 then begin // tempList.Add('cordova build android --release -- --keystore="..\android.keystore" --storePassword=android --alias=mykey'); end; end; // 執行命令 self.RunDosCommand(tempList, true); finally tempList.Free; end; end; procedure TForm2.Button8Click(Sender: TObject); var tempList: TStringList; tempCommand: string; begin try tempList := TStringList.Create; tempCommand := self.CdCordovaDirCommandStr(curProjectDir); // browser ~5.0.1 // osx ~4.0.1 // windows ~5.0.0 // www ^3.12.0 tempList.Add(tempCommand); tempList.Add('cordova requirements '); self.RunDosCommand(tempList, true); finally tempList.Free; end; end; procedure TForm2.getProjectInfoBtnClick(Sender: TObject); var stringList: TStringList; tempStr: string; projectDisk: string; // 工程目錄存放磁碟的碟符 tempProjectPath: string; begin // 通過工程全路徑名稱得到所在磁碟碟符 projectDisk := ExtractFileDrive(curProjectDir); // 得到去除碟符之後的路徑名稱 tempProjectPath := rightStr(curProjectDir, length(curProjectDir) - 3); // 命令例子:cd cordovaTest\test\HelloCordova if projectDisk = 'C:' then begin tempStr := 'cd ' + curProjectDir; end else begin tempStr := 'cd ' + tempProjectPath; end; // 建立並執行檢視工程資訊命令 try stringList := TStringList.Create; stringList.Add(projectDisk); stringList.Add(tempStr); stringList.Add('echo 工程資訊:'); stringList.Add(DosBlankLine); stringList.Add('cordova info'); // 測試資訊 stringList.Add(DosBlankLine); stringList.Add('cordova platform ls'); self.RunDosCommand(stringList, true); finally stringList.Free; end; end; procedure TForm2.getPlatformBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; begin try tempList := TStringList.Create; tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); tempList.Add('cordova platform ls'); self.RunDosCommand(tempList, true); finally tempList.Free; end; end; procedure TForm2.checkToolBtnClick(Sender: TObject); var ItemIndex: Integer; // 工具選中ID begin ItemIndex := checkToolRGroup.ItemIndex; if ItemIndex = -1 then begin showmessage('請選擇要檢測的工具!'); end else begin // 如果沒有專案選中,則提示使用者選擇 if ItemIndex = 0 then begin // 檢視node.js版本 DosCommand := 'node -v'; // 執行DOS命令 self.RunDosCommand(DosCommand, true); end else if ItemIndex = 1 then begin // 檢視phonegap版本 DosCommand := 'git --version'; // 執行DOS命令 self.RunDosCommand(DosCommand, true); end else if ItemIndex = 2 then begin // 檢視cordova版本 DosCommand := 'cordova -v'; // 執行DOS命令 self.RunDosCommand(DosCommand, true); end; end; end; procedure TForm2.clearLogBtnClick(Sender: TObject); begin logMemo.Clear; // projectDictionary.Add('ccc', 'e:\abc\def'); // projectDictionary.Add('dddd', 'f:\uio\wer'); // self.SyncProjectes(projectDictionary, projectListBox, iniFileName, true); // self.SyncProjectes(projectDictionary, projectListBox, iniFileName, false); // MyLog(projectDictionary.ToString); // MyLog(IntToStr(projectDictionary.Count)); end; procedure TForm2.configGradleBtnClick(Sender: TObject); var GradleZipFileName: string; // gradle下載檔名 GradleSaveDir: string; // Gradle最終保持目錄名稱 destFile: string; // 存放gradle的檔名 userHome: string; // 使用者目錄 begin if OpenDialog1.Execute then begin // 得到使用者目錄 userHome := StringReplace(self.GetAppDir, 'AppData\Roaming', '', [rfReplaceAll]); // Gradle最終保持目錄名稱 GradleSaveDir := userHome + GradleDirInUserHome; { 1,準備gradle存放目錄 } // 刪除非空的gradle存放目錄 if TDirectory.Exists(GradleSaveDir) = true then begin TDirectory.Delete(GradleSaveDir, true); end; // 建立gradle存放 TDirectory.CreateDirectory(GradleSaveDir); // 2,拷貝下載檔案到gradle存放目錄 GradleZipFileName := ExtractFileName(OpenDialog1.FileName); destFile := GradleSaveDir + '\' + GradleZipFileName; TFile.Copy(trim(OpenDialog1.FileName), destFile); MyLog('gradle配置完畢!'); end; end; procedure TForm2.deleteProjectBtnClick(Sender: TObject); var delProjectName: string; // 要刪除工程名稱 iniFile: TInifile; begin if projectListBox.ItemIndex <> -1 then begin // 得到要刪除工程名稱 delProjectName := trim(projectListBox.Items[projectListBox.ItemIndex]); try iniFile := TInifile.Create(iniFileName); // 1,從全部被管理工程裡面刪除當前要刪除的工程 iniFile.DeleteKey('projectes', delProjectName); // 同步資料 self.SyncProjectes(projectDictionary, projectListBox, iniFileName, false); { 如果刪除工程是當前管理工程,則進一步處理, A,清理當前工程全域性變數 B,從程式介面上消除顯示資訊 c,刪除ini配置資訊 } if delProjectName = curProjectName then begin // A,清理當前工程全域性變數 curProjectPath := ''; curProjectName := ''; curProjectDir := ''; // B,從程式介面上消除顯示資訊 curProjectNameEdt.Clear; projectPathEdt.Clear; // c,刪除ini配置資訊 iniFile.WriteString('curProject', 'name', ''); iniFile.WriteString('curProject', 'path', ''); end; finally iniFile.Free; end; end; end; procedure TForm2.doChcpBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; ItemIndex: Integer; // 選中專案ID begin // 得到選中專案ID ItemIndex := chcpRGroup.ItemIndex; // 有專案選中,則執行具體操作 if ItemIndex <> -1 then begin try tempList := TStringList.Create; if ItemIndex = 3 then begin tempList.Add('npm install -g cordova-hot-code-push-cli'); end else begin // 1, 加入進入當前工程目錄命令 tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); case ItemIndex of 0: begin tempList.Add('cordova-hcp build'); end; 1: begin tempList.Add('cordova-hcp server'); end; 2: begin tempList.Add('cordova-hcp init'); end; end; end; // 執行命令 self.RunDosCommand(tempList, true); finally tempList.Free; end; end; end; procedure TForm2.downloadGradleBtnClick(Sender: TObject); const // Gradle配置檔名稱 GradleWapperFile : string = '\platforms\android\gradle\wrapper\gradle-wrapper.properties'; GradleUrlKey: string = 'distributionUrl'; // 下載Gradle的網址key var WapperFile: string; // Gradle配置檔案權路徑名稱 tempList: TStringList; tempUrl: string; begin WapperFile := curProjectDir + GradleWapperFile; // 如果Gradle配置檔案存在,則獲取下載url,並且下載對應版本的Gradle if TFile.Exists(WapperFile) = true then begin try tempList := TStringList.Create; tempList.LoadFromFile(WapperFile); // 得到配置檔案中當前版本的gradle下載地址 tempUrl := tempList.Values[GradleUrlKey]; // 去掉"\" tempUrl := StringReplace(tempUrl, '\', '', [rfReplaceAll]); // 開啟瀏覽器下載gradle ShellExecute(0, nil, PChar(tempUrl), nil, nil, 1); finally tempList.Free; end; end else begin showmessage('Gradle配置檔案不存在,請確認是否添加了Android平臺,如果未新增,請新增後執行此操作!'); end; end; procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction); begin // 關閉開啟的DOS視窗 self.KillDosWindow; // 釋放被管理工程Dictionary projectDictionary.Free; end; procedure TForm2.FormCreate(Sender: TObject); var iniFile: TInifile; tempList: TStringList; i: Integer; begin // 是否選中外掛預設值為false isSelectPlugin := false; // 是否選中平臺預設值為false isSelectPlatfom := false; // -------------------------------------------- // 從ini檔案獲取被管理的工程 // 被管理全部工程的Dictionary projectDictionary := TDictionary<string, string>.Create; try // ini配置檔案 iniFileName := ExtractFilePath(ParamStr(0)) + configFile; // 例項化inifile類 iniFile := TInifile.Create(iniFileName); { A,得到當前正在管理工程資訊 } // 1,得到cordova工程目錄 curProjectPath := iniFile.ReadString('curProject', 'path', ''); // 2,得到工程名稱 curProjectName := iniFile.ReadString('curProject', 'name', ''); curProjectNameEdt.Text := curProjectName; // 工程目錄 // curProjectDir := curProjectPath + '\' + curProjectName; if trim(curProjectName) <> '' then begin if rightStr(curProjectPath, 1) = '\' then begin // 工程全路徑名稱 curProjectDir := curProjectPath + curProjectName; end else begin // 工程全路徑名稱 curProjectDir := curProjectPath + '\' + curProjectName; end; // 顯示工程目錄 projectPathEdt.Text := curProjectDir; end; { B,得到全部被管理工程資訊 } tempList := TStringList.Create; iniFile.ReadSectionValues('projectes', tempList); // 如果有被管理網站,則繼續處理 if tempList.Count > 0 then begin // 遍歷被管理網站 for i := 0 to tempList.Count - 1 do begin // 加入管理Dictionary中 projectDictionary.Add(tempList.KeyNames[i], tempList.ValueFromIndex[i]); // 在程式介面顯示被管理網站名稱 projectListBox.Items.Add(tempList.KeyNames[i]); end; end; finally iniFile.Free; tempList.Free; end; end; procedure TForm2.installCordovaBtnClick(Sender: TObject); begin if MessageBox(0, 'node.js和git已經都安裝完畢了嗎?', '提醒', MB_YESNO + MB_ICONWARNING) = IDYES then begin // // 關閉正在開啟的DOS視窗 // self.KillDosWindow; // 安裝cordova DosCommand := 'npm install -g cordova'; // 執行DOS命令 self.RunDosCommand(DosCommand, true); end; end; // log方法 procedure TForm2.MyLog(tempLog: string); var temp: string; oldLog: string; begin if trim(tempLog) <> '' then begin oldLog := trim(logMemo.Text); logMemo.Clear; temp := FormatDateTime('yyyy-mm-dd hh:mm:ss', now) + ' ' + trim(tempLog); if oldLog = '' then begin logMemo.Lines.Add(temp); logMemo.Lines.Add(''); end else begin logMemo.Lines.Add(temp); logMemo.Lines.Add(''); logMemo.Lines.Add(oldLog); end; end; end; procedure TForm2.removePlatformBtnClick(Sender: TObject); var tempList: TStringList; tempCommand: string; platformStr: string; // 要新增平臺命令字串 begin // 得到選中平臺 platformStr := trim(self.GetSelectedPlatform); if platformStr <> '' then begin try tempList := TStringList.Create; // 加入進入工程目錄的DOS命令 tempCommand := self.CdCordovaDirCommandStr(curProjectDir); tempList.Add(tempCommand); // 加入新增平臺的cordova命令 tempCommand := 'cordova platform rm ' + platformStr + ' --save'; tempList.Add(tempCommand); // 執行所有命令 self.RunDosCommand(tempList, true); finally tempList.Free; end; end else begin showmessage('請選擇要刪除的平臺,隨後執行刪除平臺操作!'); end; // try // tempList := TStringList.Create; // // tempCommand := self.CdCordovaDirCommandStr(projectDir); // // tempList.Add(tempComm