Class: Fastlane::Helper::GsheetMetadataHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/gsheet_metadata/helper/gsheet_metadata_helper.rb

Class Method Summary collapse

Class Method Details

.metadataObject

This method will generate all required metadata



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
# File 'lib/fastlane/plugin/gsheet_metadata/helper/gsheet_metadata_helper.rb', line 35

def self.
  
  UI.message "Let's start to generate file for every languages...."

 # Check first if we have credential to authenticate to Google Drive
  credentials = Dir.glob(OUTPUT_DIRECTORY + "/credentials.json").first

  # If no, we ask the path
  if !credentials
    
    input = UI.input("Please provide the entire path of your credential JSON:")

    File.open(OUTPUT_DIRECTORY + "/credentials.json", "w") { |file|
        file.write(File.read(input))
    }
    credentials = input

  end

  # Authenticate a session with your Service Account
  session = GoogleDrive::Session.(credentials)

  # Check if there's a spreadsheet name save
  if !File.file?(OUTPUT_DIRECTORY + "/spreadsheet.txt")
      writeSpreadsheetName(session)
  else
    reuse = UI.select("Would you like to use the file '#{File.read(OUTPUT_DIRECTORY + "/spreadsheet.txt")}'?", ["yes (y)", "no (n)"])

    if reuse == "n" || reuse == "no" || reuse == "no (n)"
        writeSpreadsheetName(session)
    end

  end

  # Get spreadsheet by its title
  spreadsheet = session.spreadsheet_by_title(File.read(OUTPUT_DIRECTORY + "/spreadsheet.txt"))

  # Check if spreadsheet exist
  if !spreadsheet
      FileUtils.rm_f(OUTPUT_DIRECTORY + "/spreadsheet.txt")
      UI.error "Spreadsheet not found, please retry"
      exit
  end

  # Enumerate through each sheet
  spreadsheet.worksheets.each { |sheet|
      
      UI.message "Create directory with name " + sheet.title
      
      # Remove directory if exist
      FileUtils.remove_dir(OUTPUT_DIRECTORY + "/" + sheet.title, true)
      
      # Create directory for current sheet
      Dir.mkdir(OUTPUT_DIRECTORY + "/" + sheet.title)
      
      # Enumerate through each title
      sheet.rows[0].each_with_index { |title, index|
          
          # Enumerate through each content
          sheet.rows[1].each_with_index { |content, idx|
              
              if idx == index
                  
                  path = OUTPUT_DIRECTORY + "/" + sheet.title + "/" + title + ".txt"
                  
                  # Create a txt file with the title as name
                  File.open(path, "w") { |file|
                      file.write(content)
                  }
                  
              end
          }
      }
  }
  UI.success "Successfully updated your metadata !"
end

.writeSpreadsheetName(session) ⇒ Object

This method will ask the name of a spreadsheet and save it



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/gsheet_metadata/helper/gsheet_metadata_helper.rb', line 18

def self.writeSpreadsheetName(session)
  
  # Ask for name
  input = UI.input("Please write the name of your spreadsheet:")

  # Check if spreadsheet exist
  if !session.spreadsheet_by_title(input)
      UI.error "Spreadsheet not found ! Please retry"
      writeSpreadsheetName(session)
  end

  File.open(OUTPUT_DIRECTORY + "/spreadsheet.txt", "w") { |file|
      file.write(input)
  }
end