You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
7.4 KiB

  1. 'use strict';
  2. import { commands, ExtensionContext, extensions, window, workspace } from 'vscode';
  3. import { Commands, registerCommands } from './commands';
  4. import { ViewShowBranchComparison } from './config';
  5. import { Config, configuration, Configuration } from './configuration';
  6. import { CommandContext, extensionQualifiedId, GlobalState, GlyphChars, setCommandContext } from './constants';
  7. import { Container } from './container';
  8. import { GitCommit, GitService, GitUri } from './git/gitService';
  9. import { Logger, TraceLevel } from './logger';
  10. import { Messages } from './messages';
  11. import { Strings, Versions } from './system';
  12. // import { Telemetry } from './telemetry';
  13. export async function activate(context: ExtensionContext) {
  14. const start = process.hrtime();
  15. // Pretend we are enabled (until we know otherwise) and set the view contexts to reduce flashing on load
  16. setCommandContext(CommandContext.Enabled, true);
  17. Logger.configure(context, configuration.get<TraceLevel>(configuration.name('outputLevel').value), o => {
  18. if (GitUri.is(o)) {
  19. return `GitUri(${o.toString(true)}${o.repoPath ? ` repoPath=${o.repoPath}` : ''}${
  20. o.sha ? ` sha=${o.sha}` : ''
  21. })`;
  22. }
  23. if (GitCommit.is(o)) {
  24. return `GitCommit(${o.sha ? ` sha=${o.sha}` : ''}${o.repoPath ? ` repoPath=${o.repoPath}` : ''})`;
  25. }
  26. return undefined;
  27. });
  28. const gitlens = extensions.getExtension(extensionQualifiedId)!;
  29. const gitlensVersion = gitlens.packageJSON.version;
  30. const enabled = workspace.getConfiguration('git', null!).get<boolean>('enabled', true);
  31. if (!enabled) {
  32. Logger.log(`GitLens (v${gitlensVersion}) was NOT activated -- "git.enabled": false`);
  33. setCommandContext(CommandContext.Enabled, false);
  34. void Messages.showGitDisabledErrorMessage();
  35. return;
  36. }
  37. Configuration.configure(context);
  38. const cfg = configuration.get<Config>();
  39. const previousVersion = context.globalState.get<string>(GlobalState.GitLensVersion);
  40. await migrateSettings(context, previousVersion);
  41. try {
  42. await GitService.initialize();
  43. }
  44. catch (ex) {
  45. Logger.error(ex, `GitLens (v${gitlensVersion}) activate`);
  46. setCommandContext(CommandContext.Enabled, false);
  47. if (ex.message.includes('Unable to find git')) {
  48. await window.showErrorMessage(
  49. "GitLens was unable to find Git. Please make sure Git is installed. Also ensure that Git is either in the PATH, or that 'git.path' is pointed to its installed location."
  50. );
  51. }
  52. return;
  53. }
  54. Container.initialize(context, cfg);
  55. registerCommands(context);
  56. const gitVersion = GitService.getGitVersion();
  57. // Telemetry.configure(ApplicationInsightsKey);
  58. // const telemetryContext: { [id: string]: any } = Object.create(null);
  59. // telemetryContext.version = gitlensVersion;
  60. // telemetryContext['git.version'] = gitVersion;
  61. // Telemetry.setContext(telemetryContext);
  62. notifyOnUnsupportedGitVersion(gitVersion);
  63. void showWelcomePage(gitlensVersion, previousVersion);
  64. context.globalState.update(GlobalState.GitLensVersion, gitlensVersion);
  65. // Constantly over my data cap so stop collecting initialized event
  66. // Telemetry.trackEvent('initialized', Objects.flatten(cfg, 'config', true));
  67. Logger.log(
  68. `GitLens (v${gitlensVersion}${cfg.mode.active ? `, mode: ${cfg.mode.active}` : ''}) activated ${
  69. GlyphChars.Dot
  70. } ${Strings.getDurationMilliseconds(start)} ms`
  71. );
  72. }
  73. export function deactivate() {
  74. // nothing to do
  75. }
  76. async function migrateSettings(context: ExtensionContext, previousVersion: string | undefined) {
  77. if (previousVersion === undefined) return;
  78. const previous = Versions.fromString(previousVersion);
  79. try {
  80. if (Versions.compare(previous, Versions.from(9, 8, 5)) !== 1) {
  81. const name = configuration.name('views')('commitFormat').value;
  82. const value = configuration.get<string>(name);
  83. if (!/\btips\b/.test(value)) {
  84. await configuration.updateEffective(name, `\${ tips }${value}`);
  85. }
  86. }
  87. else if (Versions.compare(previous, Versions.from(9, 8, 2)) !== 1) {
  88. const name = configuration.name('views')('repositories')('showBranchComparison').value;
  89. await configuration.migrate(name, name, {
  90. migrationFn: (v: boolean) => (v === false ? false : ViewShowBranchComparison.Working)
  91. });
  92. }
  93. else if (Versions.compare(previous, Versions.from(9, 6, 3)) !== 1) {
  94. const formatMigrationFn = (v: string) => {
  95. if (v == null || v.length === 0) return v;
  96. return (
  97. v
  98. // eslint-disable-next-line no-template-curly-in-string
  99. .replace(/\$\{authorAgo\}/g, '${author}, ${ago}')
  100. // eslint-disable-next-line no-template-curly-in-string
  101. .replace(/\$\{authorAgoOrDate\}/g, '${author}, ${agoOrDate}')
  102. );
  103. };
  104. await Promise.all(
  105. [
  106. configuration.name('blame')('format').value,
  107. configuration.name('currentLine')('format').value,
  108. configuration.name('hovers')('detailsMarkdownFormat').value,
  109. configuration.name('statusBar')('format').value,
  110. configuration.name('views')('commitFormat').value,
  111. configuration.name('views')('commitDescriptionFormat').value,
  112. configuration.name('views')('stashFormat').value,
  113. configuration.name('views')('stashDescriptionFormat').value
  114. ].map(s =>
  115. configuration.migrate<string, string>(s, s, {
  116. migrationFn: formatMigrationFn
  117. })
  118. )
  119. );
  120. }
  121. }
  122. catch (ex) {
  123. Logger.error(ex, 'migrateSettings');
  124. }
  125. }
  126. function notifyOnUnsupportedGitVersion(version: string) {
  127. if (GitService.compareGitVersion('2.2.0') !== -1) return;
  128. // If git is less than v2.2.0
  129. void Messages.showGitVersionUnsupportedErrorMessage(version);
  130. }
  131. async function showWelcomePage(version: string, previousVersion: string | undefined) {
  132. try {
  133. if (previousVersion === undefined) {
  134. Logger.log('GitLens first-time install');
  135. if (Container.config.showWhatsNewAfterUpgrades) {
  136. await commands.executeCommand(Commands.ShowWelcomePage);
  137. }
  138. return;
  139. }
  140. if (previousVersion !== version) {
  141. Logger.log(`GitLens upgraded from v${previousVersion} to v${version}`);
  142. }
  143. const [major, minor] = version.split('.');
  144. const [prevMajor, prevMinor] = previousVersion.split('.');
  145. if (
  146. (major === prevMajor && minor === prevMinor) ||
  147. // Don't notify on downgrades
  148. (major < prevMajor || (major === prevMajor && minor < prevMinor))
  149. ) {
  150. return;
  151. }
  152. if (Container.config.showWhatsNewAfterUpgrades && major !== prevMajor) {
  153. await commands.executeCommand(Commands.ShowWelcomePage);
  154. }
  155. else {
  156. await Messages.showWhatsNewMessage(version);
  157. }
  158. }
  159. finally {
  160. void (await Messages.showSetupViewLayoutMessage(previousVersion));
  161. }
  162. }