-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typescript template generator (not defaulted)
Adds all of the plumbing to support a .ts file template generator. In the spirit of maintiaining existing functionality, we do not default the default javascript generator to typescript. However, anyone can opt into this functionality by adding the following to their `config/application.rb`: ```ruby config.generators do |g| g.javascript_engine :typescript end ```
- Loading branch information
Mark Faga
committed
Aug 26, 2016
1 parent
2c8c006
commit e4fa4ea
Showing
11 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/rails/generators/typescript/assets/assets_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "rails/generators/named_base" | ||
|
||
module Typescript | ||
module Generators | ||
class AssetsGenerator < ::Rails::Generators::NamedBase | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
def copy_typescript | ||
template "javascript.ts", File.join('app/assets/javascripts', class_path, "#{file_name}.ts") | ||
end | ||
end | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
lib/rails/generators/typescript/assets/templates/javascript.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the behaviors and hooks related to the matching controller here. | ||
// All this logic will automatically be available in application.js. | ||
// You can use TypeScript in this file: www.typescriptlang.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
require 'rails/engine' | ||
require 'rails/generators' | ||
require 'typescript/rails/js_hook' | ||
|
||
class Typescript::Rails::Engine < Rails::Engine | ||
# For now, let's not be the default generator ... | ||
# config.app_generators.javascript_engine :ts | ||
end | ||
# To become the default generator... | ||
# config.app_generators.javascript_engine :typescript | ||
|
||
if config.respond_to?(:annotations) | ||
config.annotations.register_extensions(".ts") { |annotation| /#\s*(#{annotation}):?\s*(.*)$/ } | ||
end | ||
|
||
initializer 'override js_template hook' do |app| | ||
if app.config.generators.rails[:javascript_engine] == :typescript | ||
::Rails::Generators::NamedBase.send :include, Typescript::Rails::JsHook | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Typescript | ||
module Rails | ||
module JsHook | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
no_tasks do | ||
redefine_method :js_template do |source, destination| | ||
template(source + '.ts', destination + '.ts') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Typescript | ||
module Rails | ||
VERSION = '0.6.2.3' | ||
VERSION = '0.6.2.4' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'test_helper' | ||
require 'rails/generators/typescript/assets/assets_generator' | ||
|
||
class AssetGeneratorTest < Rails::Generators::TestCase | ||
tests Typescript::Generators::AssetsGenerator | ||
|
||
destination File.expand_path("../tmp", __FILE__) | ||
setup :prepare_destination | ||
|
||
def test_assets | ||
run_generator %w(posts) | ||
assert_no_file "app/assets/javascripts/posts.js" | ||
assert_file "app/assets/javascripts/posts.ts" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'test_helper' | ||
require 'rails/generators/rails/controller/controller_generator' | ||
require 'rails/generators/typescript/assets/assets_generator' | ||
|
||
class ControllerGeneratorTest < Rails::Generators::TestCase | ||
tests Rails::Generators::ControllerGenerator | ||
|
||
destination File.expand_path("../tmp", __FILE__) | ||
setup do | ||
prepare_destination | ||
copy_routes | ||
end | ||
|
||
def test_assets | ||
run_generator %w(posts --javascript-engine=typescript --orm=false) | ||
assert_no_file "app/assets/javascripts/posts.js" | ||
assert_file "app/assets/javascripts/posts.ts" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'test_helper' | ||
require 'rails/generators/rails/scaffold/scaffold_generator' | ||
require 'rails/generators/typescript/assets/assets_generator' | ||
|
||
class ScaffoldGeneratorTest < Rails::Generators::TestCase | ||
tests Rails::Generators::ScaffoldGenerator | ||
|
||
destination File.expand_path("../tmp", __FILE__) | ||
setup do | ||
prepare_destination | ||
copy_routes | ||
end | ||
|
||
def test_assets | ||
run_generator %w(posts --javascript-engine=typescript --orm=false) | ||
assert_no_file "app/assets/javascripts/posts.js" | ||
assert_file "app/assets/javascripts/posts.ts" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# routes dummy file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters