你在哪里存储你的Rails应用程序的版本号?

时间:2012-01-31 01:53:34

标签: ruby-on-rails versioning semantic-versioning

在版本化rails应用程序时,我们使用了精彩的semantic versioning范例。我遇到的一个问题是,最好存储这个号码?我已经看到它存储在/libenvironment.rb

只是想知道人们对最佳做法的看法?

8 个答案:

答案 0 :(得分:42)

我的策略是让你的VCS标签为你做(git显示在这里)。

将此添加到您的application.rb

# Only attempt update on local machine
if Rails.env.development?
  # Update version file from latest git tag
  File.open('config/version', 'w') do |file|
    file.write `git describe --tags --always` # or equivalent
  end
end

config.version = File.read('config/version')

然后,您可以使用Rails.configuration.version

访问应用中任意位置的版本

答案 1 :(得分:22)

我真的不认为这有任何约定。我想这就是你看起来很自然的一切。

可以放置版本号的某些地方位于:

  • config/environment.rb
  • config/application.rb
  • config/initializers/version.rb

添加:

VERSION = '1.0.0'

无论您选择哪个选项(从上面开始),都会在应用初始化时设置VERSION常量。

对于我的博客,我只更新布局的页脚 - 因为其他地方没有使用版本号。

lib - 文件夹确实听起来有点奇怪,因为此文件夹用于存储可重复使用的模块。

答案 2 :(得分:13)

我的偏好是创建一个生成

的rake任务
# config/initializers/version.rb
VERSION = ["1", "0", "f3d9da7"]

FWIW,我的佣金任务:

task :create_version do
  desc "create VERSION.  Use MAJOR_VERSION, MINOR_VERSION, BUILD_VERSION to override defaults"

  version_file = "#{Rails.root}/config/initializers/version.rb"
  major = ENV["MAJOR_VERSION"] || 1
  minor = ENV["MINOR_VERSION"] || 0
  build = ENV["BUILD_VERSION"] || `git describe --always --tags`
  version_string = "VERSION = #{[major.to_s, minor.to_s, build.strip]}\n"
  File.open(version_file, "w") {|f| f.print(version_string)}
  $stderr.print(version_string)
end

答案 3 :(得分:6)

使用rake任务通过rake进行自动化控制,例如:{{3p>

然后在config / initializers / version.rb中: 模块SiteInfo类应用程序

def self.version
  "v#{self.read_version}"
end

def self.read_version
  begin
    File.read 'VERSION'
  rescue
    raise "VERSION file not found or unreadable."
  end
end

答案 4 :(得分:3)

我个人更喜欢在应用程序类中添加一个常量。

# file: config/initializers/version.rb
class SomeApp::Application
  Version = '1.0.0'
end

答案 5 :(得分:3)

在Rails 4中,@ fearless_fool上面的rake任务需要看起来像这样:

desc "create VERSION.  Use MAJOR_VERSION, MINOR_VERSION, BUILD_VERSION to override defaults"
task :create_version do
  version_file = "#{Rails.root}/config/initializers/version.rb"
  major = ENV["MAJOR_VERSION"] || 1
  minor = ENV["MINOR_VERSION"] || 0
  build = ENV["BUILD_VERSION"] || `git describe --always --tags`
  version_string = "VERSION = #{[major.to_s, minor.to_s, build.strip]}\n"
  File.open(version_file, "w") {|f| f.print(version_string)}
  $stderr.print(version_string)
end

desc行必须存在且必须在<{em> task :create...行之前才能让rake识别它。

答案 6 :(得分:0)

version.rake

def valid? version
  pattern = /^\d+\.\d+\.\d+(\-(dev|beta|rc\d+))?$/
  raise "Tried to set invalid version: #{version}".red unless version =~ pattern
end

def correct_version version
  ver, flag = version.split '-'
  v = ver.split '.'
  (0..2).each do |n|
    v[n] = v[n].to_i
  end
  [v.join('.'), flag].compact.join '-'
end

def read_version
  begin 
    File.read 'VERSION'
  rescue
    raise "VERSION file not found or unreadable.".red
  end
end

def write_version version
  valid? version
  begin
    File.open 'VERSION', 'w' do |file|
      file.write correct_version(version)
    end
  rescue
    raise "VERSION file not found or unwritable.".red
  end
end

def reset current, which
  version, flag = current.split '-'
  v = version.split '.'
  which.each do |part|
    v[part] = 0
  end
  [v.join('.'), flag].compact.join '-'
end

def increment current, which
  version, flag = current.split '-'
  v = version.split '.'
  v[which] = v[which].to_i + 1
  [v.join('.'), flag].compact.join '-'
end

desc "Prints the current application version"
version = read_version
task :version do
  puts <<HELP
Available commands are:
-----------------------
rake version:write[version]   # set custom version in the x.x.x-? format
rake version:patch            # increment the patch x.x.x+1 (keeps any flags on)
rake version:minor            # increment minor and reset patch x.x+1.0 (keeps any flags on)
rake version:major            # increment major and reset others x+1.0.0 (keeps any flags on)
rake version:dev              # set the dev flag on x.x.x-dev
rake version:beta             # set the beta flag on x.x.x-beta
rake version:rc               # set or increment the rc flag x.x.x-rcX
rake version:release          # removes any flags from the current version

HELP
  puts "Current version is: #{version.green}"
end

namespace :version do

  desc "Write version explicitly by specifying version number as a parameter"
  task :write, [:version] do |task, args|
    write_version args[:version].strip
    puts "Version explicitly written: #{read_version.green}"
  end

  desc "Increments the patch version"
  task :patch do
    new_version = increment read_version, 2
    write_version new_version
    puts "Application patched: #{new_version.green}"
  end

  desc "Increments the minor version and resets the patch"
  task :minor do
    incremented = increment read_version, 1
    new_version = reset incremented, [2]
    write_version new_version
    puts "New version released: #{new_version.green}"
  end

  desc "Increments the major version and resets both minor and patch"
  task :major do
    incremented = increment read_version, 0
    new_version = reset incremented, [1, 2]
    write_version new_version
    puts "Major application version change: #{new_version.green}. Congratulations!"
  end

  desc "Sets the development flag on"
  task :dev do
    version, flag = read_version.split '-'
    new_version = [version, 'dev'].join '-'
    write_version new_version
    puts "Version in development: #{new_version.green}"
  end

  desc "Sets the beta flag on"
  task :beta do
    version, flag = read_version.split '-'
    new_version = [version, 'beta'].join '-'
    write_version new_version
    puts "Version in beta: #{new_version.green}"
  end

  desc "Sets or increments the rc flag"
  task :rc do
    version, flag = read_version.split '-'
    rc = /^rc(\d+)$/.match flag
    if rc
      new_version = [version, "rc#{rc[1].to_i+1}"].join '-'
    else
      new_version = [version, 'rc1'].join '-'
    end
    write_version new_version
    puts "New version release candidate: #{new_version.green}"
  end

  desc "Removes any version flags"
  task :release do
    version, flag = read_version.split '-'
    write_version version
    puts "Released stable version: #{version.green}"
  end

end

答案 7 :(得分:0)

我们可以使用git gem并创建一个初始化程序,以使用git describe设置应用程序版本

git宝石添加到开发组中。

# Gemfile
# ...
group :development do
  gem 'git'
  # ...
end

别忘了运行bundle

创建一个新的初始化程序文件。

# config/initializers/version.rb

if Rails.env.development?
  g = Git.open(Rails.root)
  version = g.describe
  puts "Setting application version to #{version}"
  File.write('config/VERSION', version)
end

module MyApp
  VERSION = File.read('config/VERSION').strip
end

现在我们可以像这样访问版本了:

➤  rails c
Setting application version to v2.1.3-7-gd5d8ea1
Loading development environment (Rails 5.2.3)
jruby-9.2.6.0 :001 > MyApp::VERSION
 => "v2.1.3-7-gd5d8ea1"