#!/usr/bin/env ruby # # Adds a new string to res/values/strings.xml for Android projects # # Usage: add_string + # require 'fileutils' def print_help STDERR.puts 'Usage: ' + File.basename($0) + ' * +' STDERR.puts 'where options include' STDERR.puts ' -k || -key convert all the words into one lowercase/_-delimited key' STDERR.puts '-h || -help print this message' exit 1 return 0 end def add_string(key,str) # Escape strings str = str.gsub /'/, "\\'" str = str.gsub /"/, "\\\"" # Allow keys like '@string/request_a_feature' key = key.gsub /@string\//,'' new_file = 'strings.xml' old_file = File.join 'res','values','strings.xml' File.open new_file,'w' do |out| IO.foreach old_file do |line| if line =~ /<\/resources>/ new_str = '' + str + '' out.puts ' ' + new_str else line.scan /name="([^"]+)"/ do |res| k = res[0] if k == key STDERR.puts 'This key already found!' return 1 end end line.scan />([^<]+)