Skip to content

Commit 0a62f07

Browse files
committed
Create a build environment for Chrome Extension
For developers: ``` $ git clone https://github.com/rails/web-console.git $ cd web-console/extensions $ bundle install $ npm run crx => Chrome will be launched with the extension. ``` Some gulp tasks are provided: * gulp crx - Build chrome extension. * gulp crx/zip - Generate a .zip file for Chrome Web Store * gulp crx/pkg - Generate a .crx file.
1 parent cf2ca5b commit 0a62f07

File tree

9 files changed

+170
-0
lines changed

9 files changed

+170
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ test/dummy/tmp/
99
test/dummy/.sass-cache
1010
Gemfile.lock
1111
node_modules/
12+
dist/
13+
tmp/

brx/img/icon_128.png

-3.53 KB
Binary file not shown.

extensions/README.markdown

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Web Console Browser Extensions
2+
3+
## Development
4+
5+
### Quickstart
6+
7+
```shell
8+
$ git clone https://github.com/rails/web-console.git
9+
$ cd web-console/brx
10+
$ bundle install
11+
$ npm run crx
12+
```

extensions/gulpfile.coffee

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
global.gulp = require("gulp")
2+
3+
path = require("path")
4+
5+
global.templatePath = (target = "")->
6+
path.resolve path.join(__dirname, "../lib/web_console/templates", target)
7+
8+
global.distpath = (target = "")->
9+
path.resolve path.join(__dirname, "./dist", target)
10+
11+
# load tasks
12+
glob = require("glob").sync
13+
glob(path.join __dirname, "gulpfiles/*.coffee").forEach require

extensions/gulpfiles/crx.coffee

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Tasks to build chrome extension.
2+
do (buildTask = "crx")->
3+
taskName = (task)-> "#{buildTask}/#{task}"
4+
output = (path = "")-> distpath "crx/#{path}"
5+
6+
# Build a chrome extension. Compiled sources is generated
7+
# into a directory called "dist/crx/".
8+
gulp.task buildTask, [taskName("src")]
9+
10+
# Launch a browser with the extension.
11+
gulp.task taskName("run"), [buildTask], ->
12+
run = require("gulp-run")
13+
run("sh scripts/run_chrome.sh --load-extension=\"#{output ""}\"").exec()
14+
15+
# Generate a .zip file for Chrome Web Store.
16+
gulp.task taskName("zip"), [buildTask], ->
17+
zip = require("gulp-zip")
18+
manifest = require(distpath "crx/manifest.json")
19+
filename = "#{manifest.name}-#{manifest.version}.zip"
20+
gulp.src [output("**/*"), "!#{output "key.pem"}"]
21+
.pipe zip(filename)
22+
.pipe gulp.dest(distpath "")
23+
24+
# Generate a .crx file.
25+
gulp.task taskName("pkg"), [buildTask], ->
26+
run = require("gulp-run")
27+
manifest = require(distpath "crx/manifest.json")
28+
filename = "#{manifest.name}-#{manifest.version}.crx"
29+
run("\"$(npm bin)/crx\" pack #{output ""} -o #{distpath filename}").exec()
30+
31+
do (srcTask = "#{buildTask}/src")->
32+
taskName = (task)-> "#{srcTask}/#{task}"
33+
34+
gulp.task srcTask, [
35+
"js"
36+
"html"
37+
"img"
38+
"lib/templates"
39+
"manifest.json"
40+
].map taskName
41+
42+
gulp.task taskName("js"), ->
43+
gulp.src ["chrome/js/**/*.js"]
44+
.pipe gulp.dest(output "js/")
45+
46+
gulp.task taskName("img"), ->
47+
gulp.src ["img/**/*.png"]
48+
.pipe gulp.dest(output "img/")
49+
50+
gulp.task taskName("manifest.json"), ->
51+
gulp.src ["chrome/manifest.json"]
52+
.pipe gulp.dest(output "")
53+
54+
gulp.task taskName("lib/templates"), ["tmp/templates"], ->
55+
gulp.src ["tmp/templates/**/*.js"]
56+
.pipe gulp.dest(output "lib/")
57+
58+
gulp.task taskName("html"), ->
59+
gulp.src ["chrome/html/**/*.html"]
60+
.pipe gulp.dest(output "html/")
61+
62+
gulp.task taskName("watch"), ->
63+
gulp.watch [templatePath "**/*.erb"], [taskName "lib"]
64+
gulp.watch ["chrome/js/**/*.js"], [taskName "js"]
65+
gulp.watch ["chrome/manifest.json"], [taskName "manifest.json"]
66+
gulp.watch ["chrome/html/**/*.html"], [taskName "html"]
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "web_console/fake_middleware"
2+
3+
def root(path = "")
4+
Pathname(File.expand_path "../../../../", __FILE__).join(path).to_s
5+
end
6+
7+
def fake_view
8+
@_view ||= WebConsole::FakeMiddleware.new(
9+
view_path: root("lib/web_console/templates"),
10+
).view
11+
end
12+
13+
def method_missing(name, *args, &block)
14+
if name.to_s.start_with?("render") && fake_view.respond_to?(name)
15+
fake_view.send name, *args, &block
16+
else
17+
super
18+
end
19+
end

extensions/gulpfiles/templates.coffee

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
do (output = "tmp/templates")->
2+
taskName = (task)->
3+
"#{output}/#{task}"
4+
5+
gulp.task output, [
6+
"erb"
7+
].map taskName
8+
9+
# Pre-compile *.js.erb.
10+
gulp.task taskName("erb"), ->
11+
run = require("gulp-run")
12+
rename = require("gulp-rename")
13+
gulp.src [templatePath "**/*.js.erb"]
14+
.pipe run("bundle exec erb -r ./gulpfiles/lib/erb_helper", verbosity: 0)
15+
.pipe rename(extname: "")
16+
.pipe gulp.dest(output)

extensions/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "brx-web-console",
3+
"version": "0.0.0",
4+
"devDependencies": {
5+
"coffee-script": "^1.9.3",
6+
"crx": "^3.0.2",
7+
"glob": "^5.0.13",
8+
"gulp": "^3.9.0",
9+
"gulp-rename": "^1.2.2",
10+
"gulp-run": "^1.6.8",
11+
"gulp-zip": "^3.0.2"
12+
},
13+
"scripts": {
14+
"crx": "npm install && node \"$(npm bin)/gulp\" crx/run"
15+
},
16+
"license": "MIT"
17+
}

extensions/scripts/run_chrome.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
3+
is_command() {
4+
type "$1" > /dev/null 2>&1
5+
}
6+
7+
find_chrome_binary() {
8+
for name in "chromium" "google-chrome" "chromium-browser"; do
9+
if is_command "$name"; then
10+
echo $name
11+
return
12+
fi
13+
done
14+
}
15+
16+
CHROME_BINARY=${CHROME_BINARY:-`find_chrome_binary`}
17+
18+
if is_command $CHROME_BINARY; then
19+
echo $CHROME_BINARY "$@"
20+
$($CHROME_BINARY "$@")
21+
else
22+
echo "Chrome is not found ($CHROME_BINARY)."
23+
echo "Try 'CHROME_BINARY=path/to/chrome'."
24+
exit 1
25+
fi

0 commit comments

Comments
 (0)