class Xclinter::Xclinter

Public Instance Methods

findUnusedImages() click to toggle source
# File lib/xclinter.rb, line 53
def findUnusedImages
  project = getProject(options)
  target = getTarget(options, project)
      
  imageNamesUsed = imageNamesUsed(target)
  images = images(target)
  
  exit findUnusedImagesHelper(images, imageNamesUsed)
end
lintAll() click to toggle source
# File lib/xclinter.rb, line 11
def lintAll
  project = getProject(options)
  target = getTarget(options, project)

  stringFiles = stringsFiles(target)
  imageNamesUsed = imageNamesUsed(target)
  images = images(target)
  
  exitcode = 0

  exitcode += lintStringFilesHelper(stringFiles)
  exitcode += lintImageLiteralsHelper(images, imageNamesUsed)
  exitcode += findUnusedImagesHelper(images, imageNamesUsed)
  
  exit exitcode
end
lintImageLiterals() click to toggle source
# File lib/xclinter.rb, line 41
def lintImageLiterals
  project = getProject(options)
  target = getTarget(options, project)
  
  imageNamesUsed = imageNamesUsed(target)
  images = images(target)
  
  exit lintImageLiteralsHelper(images, imageNamesUsed)
end
lintStringFiles() click to toggle source
# File lib/xclinter.rb, line 30
def lintStringFiles
  project = getProject(options)
  target = getTarget(options, project)

  stringFiles = stringsFiles(target)

  exit lintStringFilesHelper(stringFiles)
end

Private Instance Methods

assetFiles(target) click to toggle source
# File lib/xclinter.rb, line 121
        def assetFiles(target)
  resources(target, ".xcassets")
end
codeFiles(target) click to toggle source
# File lib/xclinter.rb, line 108
        def codeFiles(target)

 codeFiles = target.source_build_phase.files.to_a.map do |pbx_build_file|
         pbx_build_file.file_ref.real_path.to_s
  end.select do |path|
     path.end_with?(".m", ".mm", ".swift")
  end.select do |path|
     File.exists?(path)
 end

  codeFiles
end
findUnusedImagesHelper(images, imageNamesUsed) click to toggle source
# File lib/xclinter.rb, line 162
        def findUnusedImagesHelper(images, imageNamesUsed)
  exitcode = 0
  
  images.each do |image|
    if not imageNamesUsed.include? image
      puts ":: warning: Xclinter findUnusedImages: \"#{image}\" is not used in any imageLiteral and might be obsolete"
      exitcode = 1
    end
  end
  
  return exitcode
end
getProject(options) click to toggle source
# File lib/xclinter.rb, line 64
        def getProject(options)
  projectPath = options[:project]

  if projectPath.nil?
    projectPath = ENV["PROJECT_FILE_PATH"]
  end

  if projectPath.nil?
    projectPath = Dir.entries('.').select { |f| f.end_with?(".xcodeproj") }.first
  end

  if projectPath.nil?
    fail "no xcodeproject found"
  end

  project = Xcodeproj::Project.open(projectPath)

  project
end
getTarget(options, project) click to toggle source
# File lib/xclinter.rb, line 84
        def getTarget(options, project)
  targetName = options[:target]

  if targetName.nil?
    targetName = ENV["TARGETNAME"]
  end

  if targetName.nil?
    targetName = project.targets.first.name
  end

  if targetName.nil?
    fail "no target found"
  end

  target = project.targets.select { |target| target.name.eql? targetName }.first

  if target.nil?
    fail 'no target'
  end

  target
end
imageNamesUsed(target) click to toggle source
# File lib/xclinter.rb, line 139
        def imageNamesUsed(target)
  imageNamesUsed = codeFiles(target).flat_map do |file|
    File.readlines(file).to_s.enum_for(:scan, /#imageLiteral\(resourceName: \\"[^"]*\\"\)/).flat_map {Regexp.last_match}.flat_map do |string|
      string.to_s.sub!('#imageLiteral(resourceName: \\"','').sub!('\")','')
    end
  end
  
  imageNamesUsed
end
images(target) click to toggle source
# File lib/xclinter.rb, line 149
        def images(target)

  imageNames = assetFiles(target).flat_map do |assetFile|
            Dir.entries(assetFile).select do |file|
                    file.end_with?(".imageset")
            end.map do |file|
                    File.basename(file,File.extname(file))
            end
    end

  imageNames
end
lintImageLiteralsHelper(images, imageNamesUsed) click to toggle source
# File lib/xclinter.rb, line 175
        def lintImageLiteralsHelper(images, imageNamesUsed)
  exitcode = 0 
  
  imageNamesUsed.each do |imageNameUsed|
    if not images.include? imageNameUsed
      puts ":: error: Xclinter lintImageLiteral: \"#{imageNameUsed}\" does not exist in asset files"
      exitcode = 1
    end
  end
  
  return exitcode
end
lintStringFilesHelper(stringFiles) click to toggle source
# File lib/xclinter.rb, line 188
        def lintStringFilesHelper(stringFiles)
  exitcode = 0 
  
  stringFiles.each do |path|
    File.foreach(path).with_index do |line, line_num|
        if not line.strip.empty? and not line.strip.end_with?(";") and not (line.strip.start_with?("/*") and line.strip.end_with?("*/"))
            puts "#{path}:#{line_num+1}: error: Xclinter lintStringFiles: semicolon missing"
            exitcode = 1
        end
    end
  end
  
  return exitcode
end
resources(target, ending) click to toggle source
# File lib/xclinter.rb, line 129
        def resources(target, ending)
  target.resources_build_phase.files.to_a.map do |pbx_build_file|
    pbx_build_file.file_ref.real_path.to_s
  end.select do |path|
   path.end_with?(ending)
  end.select do |path|
   File.exists?(path)
  end
end
stringsFiles(target) click to toggle source
# File lib/xclinter.rb, line 125
        def stringsFiles(target)
  resources(target, ".strings")
end