Class: Stringy::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/stringy/cli.rb

Instance Method Summary collapse

Instance Method Details

#updateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/stringy/cli.rb', line 16

def update()
  
  if !Dir.exists?("Base.lproj")    
    puts "Wrong directory please select the directory that contains the Base.lproj folder" 
    return
  end
  
    if !options[:verbose] then
      Thread.new do
        #set up spinner
        glyphs = ['|', '/', '-', "\\"]
        while true
          glyphs.each do |g|
            print "\r#{g}"
            sleep 0.15
          end
        end
      end
    end
    
    warningSuppressor = options[:verbose]? "" : " > /dev/null 2>&1"

    # Run Genstrings
    puts "running genstrings" if options[:verbose]
    system('find ./ -name "*.m" -o -name "*.mm" -print0 | xargs -0 genstrings -o Base.lproj'+warningSuppressor)

    # Set up some extentions
    storyboardExt = ".storyboard"
    xibExt = ".xib"
    stringsExt = ".strings"
    localeDirExt = ".lproj"

    newStringsExt=".new"

    # Loop the storyboards in base
    Dir.glob("Base.lproj/*{#{storyboardExt},#{xibExt}}") do |storyboardPath|

      # Create the base path (eg. settings)
      baseStringsPath = storyboardPath.chomp(File.extname(storyboardPath)) + stringsExt

      puts "" if options[:verbose]
      puts "Starting " + baseStringsPath if options[:verbose]

      # Check if it exists
      stringFileIsMissing = !File.file?(baseStringsPath)
      if !stringFileIsMissing
        storyboardIsNewer = File.mtime(storyboardPath) > File.mtime(baseStringsPath)
      else
        storyboardIsNewer = false
      end

      puts "String file is missing" if options[:verbose] && stringFileIsMissing
      puts "Storyboard is newer" if options[:verbose] && storyboardIsNewer

      # Create strings file only when storyboard file newer and not just been created
      if options[:force] || stringFileIsMissing || storyboardIsNewer then

        puts "Updating " + baseStringsPath if options[:verbose]

        # Get storyboard file name and folder
        storyboardDir = File.dirname(storyboardPath)

        # Get New Base strings file full path and strings file name
        stringsFile = File.basename(baseStringsPath)
        newBaseStringsPath = "#{storyboardDir}/#{stringsFile}#{newStringsExt}"

        puts "Running: ibtool --export-strings-file #{newBaseStringsPath.chomp} #{storyboardPath.chomp}" if options[:verbose]
        system("ibtool --export-strings-file #{newBaseStringsPath.chomp} #{storyboardPath.chomp}"+warningSuppressor)

        puts "Running: iconv -f UTF-16 -t UTF-8 #{newBaseStringsPath.chomp} > #{baseStringsPath.chomp}" if options[:verbose]
        system("iconv -f UTF-16 -t UTF-8 #{newBaseStringsPath.chomp} > #{baseStringsPath.chomp}")
        
        FileUtils.rm(newBaseStringsPath) if File.exists?(newBaseStringsPath)

        if options[:overwrite] then
          # Get all locale strings folder
          Dir.glob("*#{localeDirExt}") do |localeStringsDir|

            # Skip Base strings folder
            if localeStringsDir != storyboardDir

              localeStringsPath = localeStringsDir+"/"+stringsFile

              puts "Move strings file in " + localeStringsPath if options[:verbose]
              FileUtils.mkdir_p(File.dirname(localeStringsPath))
              FileUtils.cp(baseStringsPath, localeStringsPath)
            end
          end
        end
      else
        puts "No action needed" if options[:verbose]
      end
  end
  
  puts "" #just to add a line for formating
end