1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SwiftのCGRectMakeとCGPointMakeを新しい書き方に修正するスクリプト

Last updated at Posted at 2016-07-21

swiftlint autocorrectで自動修正してくれなかったので、rubyで自作しました。

以下の変換を行います。

  • CGRectMake → CGRect
  • CGPointMake → CGPoint
# ! /usr/bin/ruby
#
# # usage
#
#   chmod +x replace.rb
#   ./replace.rb ./hoge.swift > out.swift
#
file_path = File.absolute_path(ARGV[0].to_s)
unless File.exists? file_path
  abort "ファイルが存在しません. file_path = #{file_path}"
end

f = File.open(file_path, "r")
out = []
f.each_line do |line|
  line = line.gsub(/CGRectMake\((.+?), (.+?), (.+?), (.+?)\)/ , 'CGRect(x: \1, y: \2, width: \3, height: \4)')
  line = line.gsub(/CGPointMake\((.+?), (.+?)\)/, 'CGPoint(x: \1, y: \2)')
  out << line
end
print out.join()

追記

コメントで指摘を頂きましたが、以下のようなコードに対して実行すると壊れるので注意してください。

CGRectMake(someFunctionToGetX(hoge, fuga), anotherFunction(hoge, fuga), 100, 100)
1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?