git_page.iss.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. { Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. SPDX-License-Identifier: Apache-2.0 }
  3. { ------------------------------ Page to select Git ------------------------------ }
  4. #include "git_find_installed.iss.inc"
  5. var
  6. GitPage: TInputOptionWizardPage;
  7. GitPath, GitExecutablePath, GitVersion: String;
  8. GitUseExisting: Boolean;
  9. GitSelectionInstallIndex: Integer;
  10. GitSelectionCustomPathIndex: Integer;
  11. function GetGitPath(Unused: String): String;
  12. begin
  13. Result := GitPath;
  14. end;
  15. function GitInstallRequired(): Boolean;
  16. begin
  17. Result := not GitUseExisting;
  18. end;
  19. function GitVersionSupported(Version: String): Boolean;
  20. var
  21. Major, Minor: Integer;
  22. begin
  23. Result := False;
  24. if not VersionExtractMajorMinor(Version, Major, Minor) then
  25. begin
  26. Log('GitVersionSupported: Could not parse version=' + Version);
  27. exit;
  28. end;
  29. { Need at least git 2.12 for 'git clone --reference' to work with submodules }
  30. if (Major = 2) and (Minor >= 12) then Result := True;
  31. if (Major > 2) then Result := True;
  32. end;
  33. procedure GitCustomPathUpdateEnabled();
  34. var
  35. Enable: Boolean;
  36. begin
  37. if GitPage.SelectedValueIndex = GitSelectionCustomPathIndex then
  38. Enable := True;
  39. ChoicePageSetInputEnabled(GitPage, Enable);
  40. end;
  41. procedure OnGitPagePrepare(Sender: TObject);
  42. var
  43. Page: TInputOptionWizardPage;
  44. FullName: String;
  45. i, Index, FirstEnabledIndex: Integer;
  46. OfferToInstall: Boolean;
  47. VersionToInstall: String;
  48. VersionSupported: Boolean;
  49. begin
  50. Page := TInputOptionWizardPage(Sender);
  51. Log('OnGitPagePrepare');
  52. if Page.CheckListBox.Items.Count > 0 then
  53. exit;
  54. FindInstalledGitVersions();
  55. VersionToInstall := '{#GitVersion}';
  56. OfferToInstall := True;
  57. FirstEnabledIndex := -1;
  58. for i := 0 to InstalledGitVersions.Count - 1 do
  59. begin
  60. VersionSupported := GitVersionSupported(InstalledGitVersions[i]);
  61. FullName := InstalledGitDisplayNames.Strings[i];
  62. if not VersionSupported then
  63. begin
  64. FullName := FullName + ' (unsupported)';
  65. end;
  66. FullName := FullName + #13#10 + InstalledGitExecutables.Strings[i];
  67. Index := Page.Add(FullName);
  68. if not VersionSupported then
  69. begin
  70. Page.CheckListBox.ItemEnabled[Index] := False;
  71. end else begin
  72. if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
  73. end;
  74. if InstalledGitVersions[i] = VersionToInstall then
  75. begin
  76. OfferToInstall := False;
  77. end;
  78. end;
  79. if OfferToInstall then
  80. begin
  81. Index := Page.Add('Install Git ' + VersionToInstall);
  82. if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
  83. GitSelectionInstallIndex := Index;
  84. end;
  85. Index := Page.Add('Custom git.exe location');
  86. if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
  87. GitSelectionCustomPathIndex := Index;
  88. Page.SelectedValueIndex := FirstEnabledIndex;
  89. GitCustomPathUpdateEnabled();
  90. end;
  91. procedure OnGitSelectionChange(Sender: TObject);
  92. var
  93. Page: TInputOptionWizardPage;
  94. begin
  95. Page := TInputOptionWizardPage(Sender);
  96. Log('OnGitSelectionChange index=' + IntToStr(Page.SelectedValueIndex));
  97. GitCustomPathUpdateEnabled();
  98. end;
  99. function OnGitPageValidate(Sender: TWizardPage): Boolean;
  100. var
  101. Page: TInputOptionWizardPage;
  102. Version, ErrStr: String;
  103. begin
  104. Page := TInputOptionWizardPage(Sender);
  105. Log('OnGitPageValidate index=' + IntToStr(Page.SelectedValueIndex));
  106. if Page.SelectedValueIndex = GitSelectionInstallIndex then
  107. begin
  108. GitUseExisting := False;
  109. GitExecutablePath := '';
  110. GitPath := '';
  111. GitVersion := '{#GitVersion}';
  112. Result := True;
  113. end else if Page.SelectedValueIndex = GitSelectionCustomPathIndex then
  114. begin
  115. GitPath := ChoicePageGetInputText(Page);
  116. GitExecutablePath := GitPath + '\git.exe';
  117. if not FileExists(GitExecutablePath) then
  118. begin
  119. MsgBox('Can not find git.exe in ' + GitPath, mbError, MB_OK);
  120. Result := False;
  121. exit;
  122. end;
  123. if not GetVersionOfGitExe(GitExecutablePath, Version, ErrStr) then
  124. begin
  125. MsgBox('Can not determine version of git.exe.' + #13#10
  126. + 'Please check that this copy of git works from cmd.exe.', mbError, MB_OK);
  127. Result := False;
  128. exit;
  129. end;
  130. Log('Version of ' + GitExecutablePath + ' is ' + Version);
  131. if not GitVersionSupported(Version) then
  132. begin
  133. MsgBox('Selected git version (' + Version + ') is not supported.', mbError, MB_OK);
  134. Result := False;
  135. exit;
  136. end;
  137. Log('Version of git is supported');
  138. GitUseExisting := True;
  139. GitVersion := Version;
  140. end else begin
  141. GitUseExisting := True;
  142. GitExecutablePath := InstalledGitExecutables[Page.SelectedValueIndex];
  143. GitPath := ExtractFilePath(GitExecutablePath);
  144. GitVersion := InstalledGitVersions[Page.SelectedValueIndex];
  145. Result := True;
  146. end;
  147. end;
  148. procedure GitExecutablePathUpdateAfterInstall();
  149. var
  150. GitInstallPath: String;
  151. begin
  152. GitInstallPath := GetInstallPath('SOFTWARE\GitForWindows', 'InstallPath');
  153. if GitInstallPath = '' then
  154. begin
  155. Log('Failed to find Git install path');
  156. exit;
  157. end;
  158. GitPath := GitInstallPath + '\cmd';
  159. GitExecutablePath := GitPath + '\git.exe';
  160. end;
  161. <event('InitializeWizard')>
  162. procedure CreateGitPage();
  163. begin
  164. GitPage := ChoicePageCreate(
  165. wpLicense,
  166. 'Git choice', 'Please choose Git version',
  167. 'Available Git versions',
  168. 'Enter custom location of git.exe',
  169. True,
  170. @OnGitPagePrepare,
  171. @OnGitSelectionChange,
  172. @OnGitPageValidate);
  173. end;