Browse Source

refactor: auto generate electron_version.h from the version file (#18866)

* refactor: auto generate electron_version.h from the version file

* Update BUILD.gn

Co-Authored-By: Jeremy Apthorp <[email protected]>
Samuel Attard 5 years ago
parent
commit
7201845894

+ 28 - 0
BUILD.gn

@@ -11,6 +11,7 @@ import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
 import("//v8/gni/snapshot_toolchain.gni")
 import("build/asar.gni")
 import("build/npm.gni")
+import("build/templated_file.gni")
 import("build/tsc.gni")
 import("build/webpack/webpack.gni")
 import("buildflags/buildflags.gni")
@@ -307,6 +308,32 @@ source_set("manifests") {
   }
 }
 
+npm_action("electron_version_args") {
+  script = "generate-version-json"
+
+  outputs = [
+    "$target_gen_dir/electron_version.args",
+  ]
+
+  args = rebase_path(outputs)
+
+  inputs = [
+    "ELECTRON_VERSION",
+    "script/generate-version-json.js",
+  ]
+}
+
+templated_file("electron_version_header") {
+  deps = [
+    ":electron_version_args",
+  ]
+
+  template = "build/templates/electron_version.tmpl"
+  output = "$target_gen_dir/electron_version.h"
+
+  args_files = get_target_outputs(":electron_version_args")
+}
+
 static_library("electron_lib") {
   configs += [ "//v8:external_startup_data" ]
   configs += [ "//third_party/electron_node:node_internals" ]
@@ -315,6 +342,7 @@ static_library("electron_lib") {
 
   deps = [
     ":atom_js2c",
+    ":electron_version_header",
     ":manifests",
     ":resources",
     "atom/common/api:mojo",

+ 1 - 1
atom/app/node_main.cc

@@ -12,7 +12,6 @@
 #include "atom/browser/node_debugger.h"
 #include "atom/common/api/electron_bindings.h"
 #include "atom/common/crash_reporter/crash_reporter.h"
-#include "atom/common/electron_version.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
 #include "atom/common/node_bindings.h"
 #include "atom/common/node_includes.h"
@@ -20,6 +19,7 @@
 #include "base/feature_list.h"
 #include "base/task/thread_pool/thread_pool.h"
 #include "base/threading/thread_task_runner_handle.h"
+#include "electron/electron_version.h"
 #include "gin/array_buffer.h"
 #include "gin/public/isolate_holder.h"
 #include "gin/v8_initializer.h"

+ 1 - 1
atom/browser/api/atom_api_net_log.cc

@@ -8,7 +8,6 @@
 
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/net/system_network_context_manager.h"
-#include "atom/common/electron_version.h"
 #include "atom/common/native_mate_converters/callback.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/node_includes.h"
@@ -16,6 +15,7 @@
 #include "chrome/browser/browser_process.h"
 #include "components/net_log/chrome_net_log.h"
 #include "content/public/browser/storage_partition.h"
+#include "electron/electron_version.h"
 #include "native_mate/dictionary.h"
 #include "native_mate/handle.h"
 #include "net/url_request/url_request_context_getter.h"

+ 1 - 1
atom/browser/browser_linux.cc

@@ -10,10 +10,10 @@
 #include "atom/browser/native_window.h"
 #include "atom/browser/window_list.h"
 #include "atom/common/application_info.h"
-#include "atom/common/electron_version.h"
 #include "base/command_line.h"
 #include "base/environment.h"
 #include "base/process/launch.h"
+#include "electron/electron_version.h"
 
 #if defined(USE_X11)
 #include "chrome/browser/ui/libgtkui/gtk_util.h"

+ 1 - 1
atom/browser/browser_win.cc

@@ -12,7 +12,6 @@
 
 #include "atom/browser/ui/win/jump_list.h"
 #include "atom/common/application_info.h"
-#include "atom/common/electron_version.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
 #include "base/base_paths.h"
 #include "base/file_version_info.h"
@@ -25,6 +24,7 @@
 #include "base/win/registry.h"
 #include "base/win/win_util.h"
 #include "base/win/windows_version.h"
+#include "electron/electron_version.h"
 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
 
 namespace atom {

+ 1 - 1
atom/common/api/electron_bindings.cc

@@ -13,7 +13,6 @@
 #include "atom/browser/browser.h"
 #include "atom/common/api/locker.h"
 #include "atom/common/application_info.h"
-#include "atom/common/electron_version.h"
 #include "atom/common/heap_snapshot.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
@@ -26,6 +25,7 @@
 #include "base/system/sys_info.h"
 #include "base/threading/thread_restrictions.h"
 #include "chrome/common/chrome_version.h"
+#include "electron/electron_version.h"
 #include "native_mate/dictionary.h"
 #include "services/resource_coordinator/public/cpp/memory_instrumentation/global_memory_dump.h"
 #include "services/resource_coordinator/public/cpp/memory_instrumentation/memory_instrumentation.h"

+ 1 - 1
atom/common/application_info.cc

@@ -5,11 +5,11 @@
 #include "atom/common/application_info.h"
 
 #include "atom/browser/browser.h"
-#include "atom/common/electron_version.h"
 #include "base/no_destructor.h"
 #include "base/strings/stringprintf.h"
 #include "chrome/common/chrome_version.h"
 #include "content/public/common/user_agent.h"
+#include "electron/electron_version.h"
 
 namespace atom {
 

+ 1 - 1
atom/common/application_info_linux.cc

@@ -10,11 +10,11 @@
 #include <memory>
 #include <string>
 
-#include "atom/common/electron_version.h"
 #include "atom/common/platform_util.h"
 #include "base/environment.h"
 #include "base/logging.h"
 #include "chrome/browser/ui/libgtkui/gtk_util.h"
+#include "electron/electron_version.h"
 
 namespace {
 

+ 1 - 1
atom/common/crash_reporter/crash_reporter.cc

@@ -8,7 +8,6 @@
 
 #include "atom/browser/browser.h"
 #include "atom/common/atom_constants.h"
-#include "atom/common/electron_version.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/native_mate_converters/map_converter.h"
 #include "base/command_line.h"
@@ -18,6 +17,7 @@
 #include "base/strings/string_split.h"
 #include "base/threading/thread_restrictions.h"
 #include "content/public/common/content_switches.h"
+#include "electron/electron_version.h"
 
 namespace crash_reporter {
 

+ 0 - 34
atom/common/electron_version.h

@@ -1,34 +0,0 @@
-// Copyright (c) 2013 GitHub, Inc.
-// Use of this source code is governed by the MIT license that can be
-// found in the LICENSE file.
-
-#ifndef ATOM_COMMON_ELECTRON_VERSION_H_
-#define ATOM_COMMON_ELECTRON_VERSION_H_
-
-#define ELECTRON_MAJOR_VERSION 7
-#define ELECTRON_MINOR_VERSION 0
-#define ELECTRON_PATCH_VERSION 0
-// clang-format off
-#define ELECTRON_PRE_RELEASE_VERSION -nightly.20190619
-// clang-format on
-
-#ifndef ELECTRON_STRINGIFY
-#define ELECTRON_STRINGIFY(n) ELECTRON_STRINGIFY_HELPER(n)
-#define ELECTRON_STRINGIFY_HELPER(n) #n
-#endif
-
-#ifndef ELECTRON_PRE_RELEASE_VERSION
-#define ELECTRON_VERSION_STRING                                          \
-  ELECTRON_STRINGIFY(ELECTRON_MAJOR_VERSION)                             \
-  "." ELECTRON_STRINGIFY(ELECTRON_MINOR_VERSION) "." ELECTRON_STRINGIFY( \
-      ELECTRON_PATCH_VERSION)
-#else
-#define ELECTRON_VERSION_STRING                                          \
-  ELECTRON_STRINGIFY(ELECTRON_MAJOR_VERSION)                             \
-  "." ELECTRON_STRINGIFY(ELECTRON_MINOR_VERSION) "." ELECTRON_STRINGIFY( \
-      ELECTRON_PATCH_VERSION) ELECTRON_STRINGIFY(ELECTRON_PRE_RELEASE_VERSION)
-#endif
-
-#define ELECTRON_VERSION "v" ELECTRON_VERSION_STRING
-
-#endif  // ATOM_COMMON_ELECTRON_VERSION_H_

+ 16 - 0
build/generate-template.py

@@ -0,0 +1,16 @@
+import json
+import sys
+from string import Template
+
+inpath = sys.argv[1]
+outpath = sys.argv[2]
+argpaths = sys.argv[3:]
+
+with open(inpath, 'r') as infile, open(outpath, 'w') as outfile:
+  data = {}
+  for argpath in argpaths:
+    with open(argpath, 'r') as argfile:
+      data.update(json.load(argfile))
+
+  s =  Template(infile.read()).substitute(data)
+  outfile.write(s)

+ 39 - 0
build/templated_file.gni

@@ -0,0 +1,39 @@
+template("templated_file") {
+  assert(defined(invoker.template), "Need template file to run")
+  assert(defined(invoker.output), "Need output file to run")
+
+  if (defined(invoker.values)) {
+    args_path = "$target_gen_dir/$target_name.args"
+    write_file(args_path, invoker.values, "json")
+  }
+
+  action(target_name) {
+    forward_variables_from(invoker,
+                           [
+                             "deps",
+                             "public_deps",
+                             "inputs",
+                             "outputs",
+                           ])
+    inputs = [
+      invoker.template,
+    ]
+    outputs = [
+      invoker.output,
+    ]
+    script = "//electron/build/generate-template.py"
+    args = [
+      rebase_path(invoker.template),
+      rebase_path(invoker.output),
+    ]
+
+    if (defined(invoker.values)) {
+      args += rebase_path(args_path)
+    }
+
+    if (defined(invoker.args_files)) {
+      args += rebase_path(invoker.args_files)
+      inputs += invoker.args_files
+    }
+  }
+}

+ 23 - 0
build/templates/electron_version.tmpl

@@ -0,0 +1,23 @@
+// Copyright (c) 2019 Slack Technologies, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ELECTRON_ELECTRON_VERSION_H
+#define ELECTRON_ELECTRON_VERSION_H
+
+#define ELECTRON_MAJOR_VERSION $major
+#define ELECTRON_MINOR_VERSION $minor
+#define ELECTRON_PATCH_VERSION $patch
+#if $has_prerelease
+#define ELECTRON_PRE_RELEASE_VERSION -$prerelease
+#endif
+
+#ifndef ELECTRON_PRE_RELEASE_VERSION
+#define ELECTRON_VERSION_STRING "$major.$minor.$patch"
+#else
+#define ELECTRON_VERSION_STRING "$major.$minor.$patch-$prerelease"
+#endif
+
+#define ELECTRON_VERSION "v" ELECTRON_VERSION_STRING
+
+#endif  // ELECTRON_ELECTRON_VERSION_H

+ 1 - 0
package.json

@@ -64,6 +64,7 @@
     "bump-version": "./script/bump-version.js",
     "check-tls": "python ./script/tls.py",
     "clang-format": "find atom/ chromium_src/ -iname *.h -o -iname *.cc -o -iname *.mm | xargs clang-format -i",
+    "generate-version-json": "node script/generate-version-json.js",
     "lint": "node ./script/lint.js && npm run lint:clang-format && npm run lint:docs",
     "lint:js": "node ./script/lint.js --js",
     "lint:clang-format": "python script/run-clang-format.py -r -c atom/ chromium_src/ || (echo \"\\nCode not formatted correctly.\" && exit 1)",

+ 0 - 20
script/bump-version.js

@@ -57,7 +57,6 @@ async function main () {
   await Promise.all([
     updateVersion(version),
     updatePackageJSON(version),
-    updateVersionH(components),
     updateWinRC(components)
   ])
 
@@ -125,25 +124,6 @@ async function commitVersionBump (version) {
   await GitProcess.exec(gitArgs, gitDir)
 }
 
-// updates electron_version.h file with new semver values
-// TODO(codebytere): auto-generate this
-async function updateVersionH (components) {
-  const filePath = path.resolve(__dirname, '..', 'atom', 'common', 'electron_version.h')
-  const data = await readFile(filePath, 'utf8')
-  const arr = data.split('\n')
-  const pre = components.pre && components.pre.length >= 2 ? `-${components.pre[0]}.${components.pre[1]}` : null
-
-  arr.forEach((item, idx) => {
-    if (item.includes('#define ELECTRON_MAJOR_VERSION')) {
-      arr[idx] = `#define ELECTRON_MAJOR_VERSION ${components.major}`
-      arr[idx + 1] = `#define ELECTRON_MINOR_VERSION ${components.minor}`
-      arr[idx + 2] = `#define ELECTRON_PATCH_VERSION ${components.patch}`
-      arr[idx + 4] = pre ? `#define ELECTRON_PRE_RELEASE_VERSION ${pre}` : '// #define ELECTRON_PRE_RELEASE_VERSION'
-    }
-  })
-  await writeFile(filePath, arr.join('\n'))
-}
-
 // updates atom.rc file with new semver values
 async function updateWinRC (components) {
   const filePath = path.resolve(__dirname, '..', 'atom', 'browser', 'resources', 'win', 'atom.rc')

+ 28 - 0
script/generate-version-json.js

@@ -0,0 +1,28 @@
+const fs = require('fs')
+const path = require('path')
+const semver = require('semver')
+
+const outputPath = process.argv[2]
+
+const currentVersion = fs.readFileSync(path.resolve(__dirname, '../ELECTRON_VERSION'), 'utf8').trim()
+
+const parsed = semver.parse(currentVersion)
+
+let prerelease = ''
+if (parsed.prerelease && parsed.prerelease.length > 0) {
+  prerelease = parsed.prerelease.join('.')
+}
+
+const {
+  major,
+  minor,
+  patch
+} = parsed
+
+fs.writeFileSync(outputPath, JSON.stringify({
+  major,
+  minor,
+  patch,
+  prerelease,
+  has_prerelease: prerelease === '' ? 0 : 1
+}, null, 2))