耙101

本文介绍了Rake的基本知识。 这是一个用Ruby编写的超级流行的构建工具。 它提供了很大的灵活性,并用于管理各种任务。 如果您使用的是Rails,我建议您深入了解一下哪些任务可以使用以及如何编写自己的任务。

主题

  • 什么是耙子?
  • 任务列表
  • 入门
  • 命名空间
  • 先决条件
  • 传递参数
  • 感兴趣的任务

什么是耙子?

多亏了Rails,Rake已经成为Ruby构建工具的事实上的标准。 它在Ruby社区中非常受欢迎。 很早以前,Rails背后的团队决定将Rake用作Rails本身的构建器,这意味着在过去下载Rails时,您还需要Rake的副本。 这样,Rake吸引了很多人。 不久之后,它被包含在Ruby(1.9)中。

Rake实际上取代了Ruby实用程序中的Unix实用工具Make。 像Rake这样的构建工具可用于自动执行各种任务-一种基本用于管理任务的软件。 它通常用于Rails中的管理任务(到目前为止,您很可能已经在其中使用过它),但是它的用例很多。 有些人用Markdown编写电子书,并设置了Rake任务,将相关文件转换为中间HTML文件,然后再将其转换为电子书格式。 使用Rake可以省去很多麻烦。

使Rake真正强大的原因是这些任务可以相互关联,并且可以彼此构建。 另外,由于它是用Ruby编写的,因此您可以为任务编写任何Ruby代码。 是否想在Rake任务中使用Ruby库? 没问题! 有趣的事实:它是下载次数最多的RubyGem ,下载次数超过1亿。 因此,绝对应该在工具带中多加一些注意。

它由已故的Ruby开发人员,演讲者和贡献者Jim Weirich构想而成。 确实,这是一个漂亮的工具,谢谢,吉姆! RIP!

任务列表

让我们来看看Rails开箱即用提供的一些任务。 我敢打赌,如果您之前没有查看过的内容,那么您会惊讶于可用的功能。 在应用程序或Rakefile的相关目录中,可以通过在shell中键入以下内容来列出它们:

rake --tasks

#or

rake -T
输出:
rake about                              # List versions of all Rails frameworks and the environment
rake assets:clean[keep]                 # Remove old compiled assets
rake assets:clobber                     # Remove compiled assets
rake assets:environment                 # Load asset compile environment
rake assets:precompile                  # Compile all the assets named in config.assets.precompile
rake cache_digests:dependencies         # Lookup first-level dependencies for TEMPLATE (like messages/show or comm...
rake cache_digests:nested_dependencies  # Lookup nested dependencies for TEMPLATE (like messages/show or comments/...
rake db:create                          # Creates the database from DATABASE_URL or config/database.yml for the cu...
rake db:drop                            # Drops the database from DATABASE_URL or config/database.yml for the curr...
rake db:fixtures:load                   # Load fixtures into the current environment's database
rake db:migrate                         # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:status                  # Display status of migrations
rake db:rollback                        # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear              # Clear a db/schema_cache.dump file
rake db:schema:cache:dump               # Create a db/schema_cache.dump file
rake db:schema:dump                     # Create a db/schema.rb file that is portable against any DB supported by AR
rake db:schema:load                     # Load a schema.rb file into the database
rake db:seed                            # Load the seed data from db/seeds.rb
rake db:setup                           # Create the database, load the schema, and initialize with the seed data ...
rake db:structure:dump                  # Dump the database structure to db/structure.sql
rake db:structure:load                  # Recreate the databases from the structure.sql file
rake db:version                         # Retrieves the current schema version number
rake doc:app                            # Generate docs for the app -- also available doc:rails, doc:guides (optio...
rake log:clear                          # Truncates all *.log files in log/ to zero bytes (specify which logs with...
rake middleware                         # Prints out your Rack middleware stack
rake notes                              # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom                       # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template                     # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update                       # Update configs and some other initially generated files (or use just upd...
rake routes                             # Print out all defined routes in match order, with names
rake secret                             # Generate a cryptographically secure secret key (this is typically used t...
rake spec                               # Run all specs in spec directory (excluding plugin specs)
rake spec:controllers                   # Run the code examples in spec/controllers
rake spec:features                      # Run the code examples in spec/features
rake spec:helpers                       # Run the code examples in spec/helpers
rake spec:models                        # Run the code examples in spec/models
rake spec:views                         # Run the code examples in spec/views
rake stats                              # Report code statistics (KLOCs, etc) from the application or engine
rake time:zones:all                     # Displays all time zones, also available: time:zones:us, time:zones:local...
rake tmp:clear                          # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions...
rake tmp:create                         # Creates tmp directories for sessions, cache, sockets, and pids

Rails应用程序中的输出令人惊讶地丰富,不是吗? 与我们熟悉的普通rake db:migraterake routes相比,您可以找到许多方便的任务,并且每天都要运行多次。

在左侧,您可以看到各种任务,在右侧,您可以看到提供的可选内容,作为对每个耙任务的描述。 如果要查看完整列表,除其他事项外,其中还包括缺少描述的任务,因此需要添加其他标志。

贝壳:
rake -T -A

#or

rake -T -all
输出:
rake about                              # List versions of all Rails frameworks and the environment
rake assets:clean[keep]                 # Remove old compiled assets
rake assets:clobber                     # Remove compiled assets
rake assets:environment                 # Load asset compile environment
rake assets:precompile                  # Compile all the assets named in config.assets.precompile
rake cache_digests:dependencies         # Lookup first-level dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake cache_digests:nested_dependencies  # Lookup nested dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake db:_dump                           # 
rake db:abort_if_pending_migrations     # 
rake db:charset                         # 
rake db:collation                       # 
rake db:create                          # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config)
rake db:create:all                      # 
rake db:drop                            # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config)
rake db:drop:all                        # 
rake db:fixtures:identify               # 
rake db:fixtures:load                   # Load fixtures into the current environment's database
rake db:forward                         # 
rake db:load_config                     # 
rake db:migrate                         # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:down                    # 
rake db:migrate:redo                    # 
rake db:migrate:reset                   # 
rake db:migrate:status                  # Display status of migrations
rake db:migrate:up                      # 
rake db:purge                           # 
rake db:purge:all                       # 
rake db:reset                           # 
rake db:rollback                        # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear              # Clear a db/schema_cache.dump file
rake db:schema:cache:dump               # Create a db/schema_cache.dump file
rake db:schema:dump                     # Create a db/schema.rb file that is portable against any DB supported by AR
rake db:schema:load                     # Load a schema.rb file into the database
rake db:schema:load_if_ruby             # 
rake db:seed                            # Load the seed data from db/seeds.rb
rake db:setup                           # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the database first)
rake db:structure:dump                  # Dump the database structure to db/structure.sql
rake db:structure:load                  # Recreate the databases from the structure.sql file
rake db:structure:load_if_sql           # 
rake db:test:clone                      # 
rake db:test:clone_schema               # 
rake db:test:clone_structure            # 
rake db:test:deprecated                 # 
rake db:test:load                       # 
rake db:test:load_schema                # 
rake db:test:load_structure             # 
rake db:test:prepare                    # 
rake db:test:purge                      # 
rake db:version                         # Retrieves the current schema version number
rake default                            # 
rake doc                                # 
rake doc/app                            # 
rake doc/app/created.rid                # 
rake doc:app                            # Generate docs for the app -- also available doc:rails, doc:guides (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake doc:clobber                        # 
rake doc:clobber_app                    # 
rake doc:clobber_rails                  # 
rake doc:guides                         # 
rake doc:rails                          # 
rake doc:reapp                          # 
rake doc:rerails                        # 
rake environment                        # 
rake html                               # 
rake html/created.rid                   # 
rake log:clear                          # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
rake magic                              # Magic rake task
rake middleware                         # Prints out your Rack middleware stack
rake no_description                     # 
rake notes                              # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom                       # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake notes:fixme                        # 
rake notes:optimize                     # 
rake notes:todo                         # 
rake rails:template                     # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:templates:copy               # 
rake rails:update                       # Update configs and some other initially generated files (or use just update:configs or update:bin)
rake rails:update:bin                   # 
rake rails:update:configs               # 
rake railties:install:migrations        # 
rake routes                             # Print out all defined routes in match order, with names
rake secret                             # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions)
rake spec                               # Run all specs in spec directory (excluding plugin specs)
rake spec:controllers                   # Run the code examples in spec/controllers
rake spec:features                      # Run the code examples in spec/features
rake spec:helpers                       # Run the code examples in spec/helpers
rake spec:models                        # Run the code examples in spec/models
rake spec:prepare                       # 
rake spec:statsetup                     # 
rake spec:views                         # Run the code examples in spec/views
rake stats                              # Report code statistics (KLOCs, etc) from the application or engine
rake time:zones:all                     # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6
rake time:zones:local                   # 
rake time:zones:us                      # 
rake tmp                                # 
rake tmp/cache                          # 
rake tmp/cache/assets                   # 
rake tmp/cache/assets/development       # 
rake tmp/cache/assets/production        # 
rake tmp/cache/assets/test              # 
rake tmp/pids                           # 
rake tmp/sessions                       # 
rake tmp/sockets                        # 
rake tmp:cache:clear                    # 
rake tmp:clear                          # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create                         # Creates tmp directories for sessions, cache, sockets, and pids
rake tmp:pids:clear                     # 
rake tmp:sessions:clear                 # 
rake tmp:sockets:clear                  #

惊喜,几乎是原来的三倍! 看一看它们,如果愿意,可以随意玩耍,但是请将重点突出显示在内存中,以备将来使用。 检查任务以查看可用的内容可能会阻止您重新发明轮子。

入门

Rakefile可以具有以下五个外观之一:

  • rakefile.rb
  • 耙文件
  • 瑞克文件
  • Rakefile.rb
  • .rake文件

通常,您会看到普通的Rakefile版本,但是类似Rails的框架需要更复杂的组织。 使用任何能使您的血液流动的东西。 如果Rakefile逻辑将任务分割成多个文件, Rakefile创建一个或.rake扩展名为.rake文件。 然后在其中任何一个中定义您的任务。

自定义Rakefile组织

Rails使这变得异常容易。 它在您应用的根目录中有一个Rakefile。 它包含以下内容:

瑞克文件
require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

当您有许多自定义任务时,将它们拆分为离散的.rake文件并将它们放置在lib/tasks更有意义。 上面的Rakefile只是加载它们,但是lib目录是执行任务的更好逻辑家。 甚至还有一个Rails生成器来自动化部分过程。 如果输入:

贝壳
rails generate some_task

=> create lib/tasks/some_task.rake

您会自动将Rakefile放置在正确的目录中。 甚至已经为您设置了任务。 真好! 在其他不使用Rails的项目中,您只需要创建一个rakelib目录并将Rakefiles放置在其中,最好使用.rake文件扩展名。 然后创建一个名为Rakefile的文件,所有这些文件已经可以使用。

耙子任务解剖

lib /任务/some_task.rake
desc 'List versions of all Rails frameworks and the environment'
task :about do
  puts 'Some magic goes in here…'
end

对于您中间的完整Ruby新手,以及来自中括号语言的人来说,这就是它的括号。

desc('List versions of all Rails frameworks and the environment')
task(:about) do
  puts('Some magic goes in here…')
end

顺便说一句,看起来很奇怪。 只需丢掉多余的括号即可,没人会这样写任务。

我们提供了一个名为task :about的描述,它不仅提醒我们将来要通过特定任务实现的目标,而且还可以在运行rake -T时显示出来。 不要偷懒。 这可能不值得。

右下方是关键字task,它定义了一个名为about的新任务。 可以通过rake about在命令行上调用它的魔力,然后调用它。 另一方面, rake :about将导致Rake中止,而不知道“如何构建任务:about”。

通过do end块,我们有了一个lambda ,它的主体指定了任务的作用。 这是任务所需的基本设置。 当然,它提供了更多选择,但是总体结构是相同的。

要求/导入

一些Rakefile
require './whatever.rb'

如果您需要包括其他Ruby文件或Rakefile,则可以通过标准的require语句轻松实现。

一些Rakefile
import 'whatever.rb'

Rake本身为我们提供了另一种方法,即import方法。 可以在Rakefile的任何行中使用它。 当您遇到麻烦时,这会有所帮助,因为所需的文件是在Rakefile加载完成并因此炸毁之前加载的。 另一方面,导入的文件将始终在Rakefile之后加载。

调用并执行

有时,您可能想从Task类中手动执行一些已定义的任务。 为此,您有Rake::Task类的两个方法: executeinvoke

Rake::Task['some_task'].invoke
Rake::Task['some_task'].execute

使用Rake::Task['some_task']代码,我们可以执行some_task Rake任务。 它返回Rake::Task类的实例,然后在其上运行任何可用的方法。

命名空间

一个很酷的功能是可以为您的任务命名空间。 您可能已经使用了数十次。 例如,在运行rake db:migrate ,您已经使用了db名称空间。 您可以通过在名称空间中用冒号:分隔任务来调用该任务。 命名空间是一种在rake文件中组织任务的简便方法-使其在逻辑上保持分隔。 顺便说一下,多个名称空间,例如rake time:zones:all都很好。

其他示例包括:

rake db:drop 
rake db:seed
rake log:clear
rake spec:views
rake spec:models  
rake db:rollback
rake spec:helpers 
rake spec:features
rake db:schema:load  
rake assets:precompile 
rake db:migrate:status
一些Rakefile
namespace :db do
  desc 'Migrating some stuff'
  task :migrate do
    ...
  end
end

这是基本设置。 实际上,它要复杂得多,甚至可以嵌套多次。 快速浏览一下Rails代码库,并亲自了解rake db:migrate是如何实现的 。 如果它在你头上,不要感到难过。 环顾四周,尝试辨别其结构,然后继续。

先决条件

组织任务并使它们保持干燥的另一种策略是使用执行任务的先决条件。 这就像一个依赖关系,必须在实际任务开始其工作之前先运行它。 这样,您可以构建更复杂的任务-根据需要进行复杂的处理。 但是我建议不要变得太聪明,并使其尽可能简单,并使其尽可能地易于理解。

一些Rakefile
task :stop_megalomaniac do
  puts 'Lots of smart talk, car chases and guns fired'
end

task :bond_saves_the_day => :stop_psychotic_megalomaniac do
  puts 'Lots of Dom Pérignon, oysters and bond girl business'
end

如果要依赖多个任务,只需将它们粘贴到一个数组中即可。 当然,放置它们的顺序很重要。

task :get_mr_wolf do
  puts "You ain’t got no problem Jules, I’m on it! Go in there and chill them out and wait for the wolf who should be coming directly!"
end

task :figure_out_bonnie_situation do
  puts "If I was informed correctly, the clock is ticking. Is that right Jimmy?"
end

task :calm_down_jimmy do
  puts "Jimmy, do me a favor, will you? I smelled some coffee back there. Would you make me a cup?"
end

task :get_vince_vega_in_line do
  puts "Come again? Get it straight buster. I’m not here to say please! I’m here to tell you what to do!"
end

task :clean_car do
  puts "I need you two fellas to take those cleaning products and clean the inside of the car. I’m talking fast, fast, fast!"
end

task :clean_crew do
  puts "Jim, the soap! O.K. gentlemen, you both been to county before I’m sure. Here it comes!"
end

task :get_rid_of_evidence_at_monster_joes do
  puts "So what’s with the outfits? You guys are going to a Volleyball game or something?"
end

task :drive_into_the_sunrise do
  puts "Call me Winston!"
end

task :solve_bonnie_situation => [:get_mr_wolf, :calm_down_jimmy, :figure_out_bonnie_situation, :get_vince_vega_in_line, :clean_car, :clean_crew, :get_rid_of_evidence_at_monster_joes, :drive_into_the_sunrise]  do
  puts "You know, I’d go for breakfast. Feel like having breakfast with me?"
end

如果您运行依赖于另一个的rake任务,我们将获得以下输出:

贝壳
$ rake solve_bonnie_situation
You ain’t got no problem Jules, I’m on it! Go in there and chill them out and wait for the wolf who should be coming directly!
Jimmy, do me a favor, will you? I smelled some coffee back there. Would you make me a cup?
If I was informed correctly, the clock is ticking. Is that right Jimmy?
Come again? Get it straight buster. I’m not here to say please! I’m here to tell you what to do!
I need you two fellas to take those cleaning products and clean the inside of the car. I’m talking fast, fast, fast!
Jim, the soap! O.K. gentlemen, you both been to county before I’m sure. Here it comes! 
So what’s with the outfits? You guys are going to a Volleyball game or something?
Call me Winston!
You know, I’d go for breakfast. Feel like having breakfast with me?

定义rake任务的顺序对输出没有影响-只是将先决任务放置在数组中以实现任务依赖性的顺序。 另外,请为此使用hashrocket =>语法。

一长串依赖关系可能是代码的味道。 如果您需要处理较长的时间,请通过将其封装在一个方法中进行清理,然后将其作为前提条件进行传递。

def mr_wolf_tasks 
  [:get_mr_wolf, :calm_down_jimmy, :figure_out_bonnie_situation, :get_vince_vega_in_line, :clean_car, :clean_crew, :get_rid_of_evidence_at_monster_joes, :drive_into_the_sunrise] 
end

...

task :solve_bonnie_situation => mr_wolf_tasks do
  puts 'You know, I’d go for breakfast. Feel like having breakfast with me?'
end

在先决条件的上下文中,要记住的一件事是,如果您不在相关名称空间之内,则只需要提及一个名称空间。

一些Rakefile
namespace :marsellus_wallace do
  task :call_winston_wolf do
    ...
  end
end

task :solve_bonnie_situation => 'marsellus_wallace:call_winston_wolf' do
  ...
end

namespace :marsellus_wallace do
  task :call_winston_wolf do
    ...
  end

  task :solve_bonnie_situation => :call_winston_wolf do
    ...
  end
end

但是,需要注意的重要一点是:如果确实需要提及名称空间,则必须以字符串=> 'marsellus_wallace:call_winston_wolf'传递先决条件。

当然,上面的示例是愚蠢的,而不是现实生活中的示例,但其目的是向您展示先决条件如何工作以及如何在必备条件彼此依赖时将它们放在一起。

传递参数

您有两个选项可以将参数传递给Rake任务:使用Bash变量或使用Rake的语法本身。

ENV变量

如果您以前没有玩过Bash,或者Bash听起来像gobbledegook,那么让我们从头开始学习5点。 Shell中的Bash提供两种变量:全局(即环境)变量和局部变量。 两者均以大写形式编写。 环境变量是全局变量,这意味着它们在所有shell中都可用,并且在您关闭它们时不会消失-与局部Bash变量不同,后者仅在当前shell中可用。

环境变量可以包含可由多个应用程序使用的数据,并且通常用作共享配置设置的便捷方法。 与此相反,局部Bash变量就是局部的。 在使用Rake的上下文中,您可以通过Ruby进行访问,并且实际上可以从命令行传递变量。

费耶

除了一点点,如果您在shell中键入envENV ,则可以访问一大堆环境变量。 我编辑了该列表,但是为了更好地理解什么是环境变量以及它们包括什么,我建议您自己运行它。

贝壳
env
输出量
TERM_PROGRAM=Apple_Terminal
TERM=screen-256color
SHELL=/bin/bash
TMUX=/private/var/folders/4z/3np9k5ks62b1xpbn_w_lmrgh0000gr/T/tmux-504/default,4146,0
EDITOR=vim
LANG=en_US.UTF-8
TMUX_PANE=%1
is_vim=echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?x?)(diff)?$"
...
...
...

如果要查看本地Bash变量的列表,可以运行set

贝壳
( set -o posix ; set ) | less

set命令为您提供了更多输出,但以上内容立即显示了相关位。

Ruby的ENV类方法

Ruby提供了一种通过类似哈希的访问器来使用环境和本地Bash变量的方法。 为了满足我们的需求,当我们将变量传递给Rake任务时,它将是一个本地Bash变量,您可以在运行set或其变体的变量列表中找到它。 Ruby可以使用ENV['VARIABLE']读出它。

贝壳
rake prepare_book BOOKTITLE='Confessions of a unicorn'

不过,我想说明的是,此变量不会添加到系统使用的ENV列表中,即您看到的从shell调用env的东西。 要将其添加到该列表,您需要将其export 。 这是另一个故事 ,但我想我应该说清楚。

一些Rakefile
task :prepare_book do
  book_title = ENV['BOOKTITLE'] || 'Working Title'
  puts "Do something with the #{book_title}"
end

在此任务定义中,您可以看到我们如何准备接受或合并传递给任务调用的变量。 Ruby的ENV[BASHVARIABLE]完成了所有繁重的工作。 但是,如果BOOKTITLE是全局环境变量,那么我们也可以使用此语法在此任务定义中访问它。

耙参数语法

第二种方法是使用纯Rake语法。 您只需将变量传递到方括号中即可。 这种方法更好,您可以使事情更加孤立。 如果Rake完全有能力处理此事,为什么还要参与Bash? 另外,您没有任何Bash变量可以那样浮动。 如果您想将多个参数传递给任务,那么也要优雅得多。

贝壳
rake "create_mi6_agent[James, Bond, 007]"
一些Rakefile
task :create_mi6_agent, [:first_name, :last_name, :number] do |t, args|
  puts "Number #{args.number} is commander #{args.first_name} #{args.last_name}."
end

当您传递的参数超过任务中定义的参数时,您可以简单地通过args访问它们。 args.extras显示所有附加传入参数的数组。 args.to_a当然会在数组中显示所有参数。

感兴趣的任务

以下是Rails随附的Rake任务的简短列表:

  • D b
  • doc
  • tmp
  • 统计资料
  • 笔记
  • 关于
  • 秘密
  • 资产
  • 路线

D b

以下是db命名空间下用于运行Active Record迁移的几个有用的任务:

rake db:version打印模式的当前版本。 输出看起来像这样:

Current version: 20160129135912

rake db:migrate运行最后一个迁移-尚未运行的迁移。 您还可以为它传递特定的迁移以运行。

贝壳
rake db:migrate VERSION=20080906120000

rake db:create创建您的数据库。 如果默认为开发和测试数据库。

db/development.sqlite3
db/test.sqlite3

rake db:test:prepare确保已在开发数据库上运行的迁移也针对测试数据库运行。 当然,如果测试数据库的架构与开发数​​据库不同步,那将不是很有用。

rake db:drop:all默认会同时删除测试数据库和开发数据库。

rake db:migrate:uprake db:migrate:down运行有问题的迁移的updown方法。

rake db:redo确保在运行迁移之后,该迁移是可逆的。 它先运行rake db:down ,然后运行rake db:up

rake db:rollback撤消上一次迁移。

rake db:drop默认情况下由开发和测试数据库删除。

rake db:reset首先删除数据库,然后通过加载架构和播种数据库来再次设置它们。

doc

rake doc:appdoc/app下生成文档。 它创建有关您的源代码HTML页面,以方便浏览。 太酷了!

屏幕截图
任务控制器的屏幕截图

rake doc:railsdoc/api也生成HTML网页的API文档。 我想如果您处于离线状态就很方便。

tmp

Rails应用程序根目录中的tmp目录是存放临时文件的地方-最重要的是用于会话和缓存的文件。 rake tmp:create可以为您提供处理临时文件所需的一切。 rake tmp:cache:clear清除tmp/cache目录。 rake tmp:sessions:clear清除tmp/sessions目录。

统计资料

rake stats为您提供了有关应用程序的不错概述。

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |    89 |    69 |       6 |      18 |   3 |     1 |
| Helpers              |    13 |    13 |       0 |       1 |   0 |    11 |
| Models               |    89 |    54 |       6 |       7 |   1 |     5 |
| Mailers              |     0 |     0 |       0 |       0 |   0 |     0 |
| Javascripts          |    25 |     0 |       0 |       0 |   0 |     0 |
| Libraries            |     0 |     0 |       0 |       0 |   0 |     0 |
| Controller specs     |    99 |    81 |       0 |       0 |   0 |     0 |
| Feature specs        |    14 |    11 |       0 |       0 |   0 |     0 |
| Helper specs         |    45 |    12 |       0 |       0 |   0 |     0 |
| Model specs          |    10 |     8 |       0 |       0 |   0 |     0 |
| View specs           |    60 |    48 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |   444 |   296 |      12 |      26 |   2 |     9 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 136     Test LOC: 160     Code to Test Ratio: 1:1.2

笔记

您可以在代码中留下注释。 您可以在注释中添加TODOFIXMEOPTIMIZE前缀。

一些Ruby文件
# FIXME Something to fix here
class Agent < ActiveRecord::Base
  belongs_to :operation
  belongs_to :mission

...

当您运行rake notes ,Rake解析这些前缀并从您的代码中获取所有这些注释的列表。

贝壳
app/models/agent.rb:
  * [1] [FIXME] Something to fix here

您甚至可以获得在其中找到它们的详细列表-目录,文件名,行号[Line Number] ,所有内容都包括在内。 太好了吧?

关于

rake about为您提供以下版本号的概述:

  • 滑轨
  • Ruby
  • Ruby
  • 数据库适配器
  • 模式版本
  • 中间件
  • 应用根

以及许多其他有用的信息。

秘密

如果您对会话密钥有偏执并且想要替换它,那么rake secret将为您生成一个新的伪随机密钥。 但是,它不会为您替换密钥。 Shell中的输出如下所示:

b40a74b94cd93fed370fd4f8f9333633c03990b7d9128383511de5240acf3fa6b6b07127e875814fdadb32f1aa44d69c4e82592b0ce97f763ea216a21e61881d

资产

rake assets:precompile可让您在public/assets下预编译public/assets 。 如果要摆脱旧资产,只需运行rake assets:clean 。 最后,运行rake assets:clobber会删除整个public/assets目录。

路线

rake routes可能是最重要的一条。 它显示了您应用中的所有路线。

最后的想法

编写Rake任务而又不知道幕后发生的事情很快就会变得无聊。 本文是对那些想第一次窥探Rake的人们的热身和介绍。

我希望它能涵盖所有基本知识,而又不要太干—毕竟,Rake是超级超级涂料,但同时,它可能不是最性感的玩具。 但是,Rake的基本知识应该为您提供所有危险的知识,并扩展您对Rake任务的了解。

翻译自: https://code.tutsplus.com/articles/rake-101--cms-26215

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值