Skip to content

Commit

Permalink
refactor: auto generate electron_version.h from the version file
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jun 18, 2019
1 parent d581160 commit 8e5e10c
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 38 deletions.
30 changes: 30 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -307,6 +308,34 @@ source_set("manifests") {
}
}

npm_action("electron_version_args") {
script = "generate-version-json"

outputs = [
"$target_gen_dir/electron_version.args",
]

args = [ rebase_path(outputs[0]) ]

inputs = [
"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"

values = {
}

extra_args = get_target_outputs(":electron_version_args")
}

static_library("electron_lib") {
configs += [ "//v8:external_startup_data" ]
configs += [ "//third_party/electron_node:node_internals" ]
Expand All @@ -315,6 +344,7 @@ static_library("electron_lib") {

deps = [
":atom_js2c",
":electron_version_header",
":manifests",
":resources",
"atom/common/api:mojo",
Expand Down
2 changes: 1 addition & 1 deletion atom/app/node_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
#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"
#include "base/command_line.h"
#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"
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_net_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

#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"
#include "base/command_line.h"
#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"
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/browser_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/browser_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion atom/common/api/electron_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion atom/common/application_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion atom/common/application_info_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion atom/common/crash_reporter/crash_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {

Expand Down
16 changes: 16 additions & 0 deletions build/generate-template.py
Original file line number Diff line number Diff line change
@@ -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(''.join(infile.readlines())).safe_substitute(data)
outfile.write(s)
35 changes: 35 additions & 0 deletions build/templated_file.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
template("templated_file") {
assert(defined(invoker.template), "Need template file to run")
assert(defined(invoker.output), "Need output file to run")
assert(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),
rebase_path(args_path),
]

if (defined(invoker.extra_args)) {
args += rebase_path(invoker.extra_args)
inputs += invoker.extra_args
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2013 GitHub, Inc.
// 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 ATOM_COMMON_ELECTRON_VERSION_H_
#define ATOM_COMMON_ELECTRON_VERSION_H_
#ifndef ELECTRON_VERSION_H_
#define 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.20190618
// clang-format on
#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_STRINGIFY
#define ELECTRON_STRINGIFY(n) ELECTRON_STRINGIFY_HELPER(n)
Expand All @@ -31,4 +31,4 @@

#define ELECTRON_VERSION "v" ELECTRON_VERSION_STRING

#endif // ATOM_COMMON_ELECTRON_VERSION_H_
#endif // ELECTRON_VERSION_H_
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
20 changes: 0 additions & 20 deletions script/bump-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ async function main () {
await Promise.all([
updateVersion(version),
updatePackageJSON(version),
updateVersionH(components),
updateWinRC(components)
])

Expand Down Expand Up @@ -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')
Expand Down
28 changes: 28 additions & 0 deletions script/generate-version-json.js
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 8e5e10c

Please sign in to comment.