Skip to content

Instantly share code, notes, and snippets.

@mironal
Created January 18, 2014 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mironal/8485561 to your computer and use it in GitHub Desktop.
Save mironal/8485561 to your computer and use it in GitHub Desktop.
#!/bin/sh
exec scala "$0" "$@"
!#
/**
CocoaPods の Pods-acknowledgements.markdown のライブラリの並び順を変更して出力するコマンド
`./update-acknowledgements-md Lib1 Lib2 path/to/file | markdown -html4tags` とかやれば HTML 形式で得られる
*/
import scala.io.Source
if (args.length == 0) {
System.err.println("引数を指定しろ")
showUsage
sys.exit(-1)
}
val preTitles = if (args.length == 1) {
Array[String]()
} else {
args.init.map(s => s"## ${s}") // markdown の形式にしておく
}
val filename = args.last
val source = Source.fromFile(filename)
val lines = source.getLines.toList.filterNot(_.startsWith("Generated by CocoaPods -"))
source.close
// # Acknowledgements の部分
val header: String = lines.takeWhile(_.startsWith("## ") == false).mkString("\n")
// ## hogehoge の部分達
val libTitles: List[String] = lines.filter(l => l.startsWith("## "))
if ( args.exists( _ == "--titles") ) {
libTitles.map(s => s.replaceAll("## ", "")).foreach(println)
sys.exit
}
// 引数チェック. 存在しないライブラリが指定されていた場合は 警告を出す
{
(preTitles diff libTitles)
.foreach(s => System.err.println(s"warn: ${s} not found."))
}
// ## hogehoge 毎に一塊にする
val contents = hoge(libTitles, lines)
// 表示
{
println(header)
// 優先的に表示するライブラリのタイトル ++ その他のライブラリのタイトル
val titles = (preTitles intersect libTitles) ++ (libTitles diff preTitles)
titles.flatMap(t => contents.find(l => l.head == t)).flatten.foreach(println)
}
// "## hogehoge" 毎にひと塊にする糞メソッド なんか他にいい方法無いの...
def hoge(headers: List[String], lines: List[String]): List[List[String]] = {
def h(rslt: List[List[String]], headers: List[String], lines: List[String]): List[List[String]] = {
headers match {
case Nil => rslt :+ lines
case x::xs => {
val a = lines.span(_ != x)
h(rslt :+ a._1, xs, a._2)
}
}
}
h(List[List[String]](), headers, lines)
}
def showUsage = {
val usage = """
-- 使い方 --
ライブラリの一覧を表示
./update-acknowledgements-md --titles path/to/file
例えば Lib1 と Lib2というライブラリを先頭に持ってきたい時は以下のように実行
./update-acknowledgements-md Lib1 Lib2 path/to/file
"""
println(usage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment