build_apple.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "bytes"
  7. "crypto/x509"
  8. "encoding/pem"
  9. "fmt"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "strings"
  15. "text/template"
  16. "golang.org/x/tools/go/packages"
  17. )
  18. func goAppleBuild(pkg *packages.Package, bundleID string, targets []targetInfo) (map[string]bool, error) {
  19. src := pkg.PkgPath
  20. if buildO != "" && !strings.HasSuffix(buildO, ".app") {
  21. return nil, fmt.Errorf("-o must have an .app for -target=ios")
  22. }
  23. productName := rfc1034Label(path.Base(pkg.PkgPath))
  24. if productName == "" {
  25. productName = "ProductName" // like xcode.
  26. }
  27. infoplist := new(bytes.Buffer)
  28. if err := infoplistTmpl.Execute(infoplist, infoplistTmplData{
  29. BundleID: bundleID + "." + productName,
  30. Name: strings.Title(path.Base(pkg.PkgPath)),
  31. }); err != nil {
  32. return nil, err
  33. }
  34. // Detect the team ID
  35. teamID, err := detectTeamID()
  36. if err != nil {
  37. return nil, err
  38. }
  39. projPbxproj := new(bytes.Buffer)
  40. if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{
  41. TeamID: teamID,
  42. }); err != nil {
  43. return nil, err
  44. }
  45. files := []struct {
  46. name string
  47. contents []byte
  48. }{
  49. {tmpdir + "/main.xcodeproj/project.pbxproj", projPbxproj.Bytes()},
  50. {tmpdir + "/main/Info.plist", infoplist.Bytes()},
  51. {tmpdir + "/main/Images.xcassets/AppIcon.appiconset/Contents.json", []byte(contentsJSON)},
  52. }
  53. for _, file := range files {
  54. if err := mkdir(filepath.Dir(file.name)); err != nil {
  55. return nil, err
  56. }
  57. if buildX {
  58. printcmd("echo \"%s\" > %s", file.contents, file.name)
  59. }
  60. if !buildN {
  61. if err := os.WriteFile(file.name, file.contents, 0644); err != nil {
  62. return nil, err
  63. }
  64. }
  65. }
  66. // We are using lipo tool to build multiarchitecture binaries.
  67. cmd := exec.Command(
  68. "xcrun", "lipo",
  69. "-o", filepath.Join(tmpdir, "main/main"),
  70. "-create",
  71. )
  72. var nmpkgs map[string]bool
  73. builtArch := map[string]bool{}
  74. for _, t := range targets {
  75. // Only one binary per arch allowed
  76. // e.g. ios/arm64 + iossimulator/amd64
  77. if builtArch[t.arch] {
  78. continue
  79. }
  80. builtArch[t.arch] = true
  81. path := filepath.Join(tmpdir, t.platform, t.arch)
  82. // Disable DWARF; see golang.org/issues/25148.
  83. if err := goBuild(src, appleEnv[t.String()], "-ldflags=-w", "-o="+path); err != nil {
  84. return nil, err
  85. }
  86. if nmpkgs == nil {
  87. var err error
  88. nmpkgs, err = extractPkgs(appleNM, path)
  89. if err != nil {
  90. return nil, err
  91. }
  92. }
  93. cmd.Args = append(cmd.Args, path)
  94. }
  95. if err := runCmd(cmd); err != nil {
  96. return nil, err
  97. }
  98. // TODO(jbd): Set the launcher icon.
  99. if err := appleCopyAssets(pkg, tmpdir); err != nil {
  100. return nil, err
  101. }
  102. // Build and move the release build to the output directory.
  103. cmdStrings := []string{
  104. "xcodebuild",
  105. "-configuration", "Release",
  106. "-project", tmpdir + "/main.xcodeproj",
  107. "-allowProvisioningUpdates",
  108. "DEVELOPMENT_TEAM=" + teamID,
  109. }
  110. cmd = exec.Command("xcrun", cmdStrings...)
  111. if err := runCmd(cmd); err != nil {
  112. return nil, err
  113. }
  114. // TODO(jbd): Fallback to copying if renaming fails.
  115. if buildO == "" {
  116. n := pkg.PkgPath
  117. if n == "." {
  118. // use cwd name
  119. cwd, err := os.Getwd()
  120. if err != nil {
  121. return nil, fmt.Errorf("cannot create .app; cannot get the current working dir: %v", err)
  122. }
  123. n = cwd
  124. }
  125. n = path.Base(n)
  126. buildO = n + ".app"
  127. }
  128. if buildX {
  129. printcmd("mv %s %s", tmpdir+"/build/Release-iphoneos/main.app", buildO)
  130. }
  131. if !buildN {
  132. // if output already exists, remove.
  133. if err := os.RemoveAll(buildO); err != nil {
  134. return nil, err
  135. }
  136. if err := os.Rename(tmpdir+"/build/Release-iphoneos/main.app", buildO); err != nil {
  137. return nil, err
  138. }
  139. }
  140. return nmpkgs, nil
  141. }
  142. func detectTeamID() (string, error) {
  143. // Grabs the first certificate for "Apple Development"; will not work if there
  144. // are multiple certificates and the first is not desired.
  145. cmd := exec.Command(
  146. "security", "find-certificate",
  147. "-c", "Apple Development", "-p",
  148. )
  149. pemString, err := cmd.Output()
  150. if err != nil {
  151. err = fmt.Errorf("failed to pull the signing certificate to determine your team ID: %v", err)
  152. return "", err
  153. }
  154. block, _ := pem.Decode(pemString)
  155. if block == nil {
  156. err = fmt.Errorf("failed to decode the PEM to determine your team ID: %s", pemString)
  157. return "", err
  158. }
  159. cert, err := x509.ParseCertificate(block.Bytes)
  160. if err != nil {
  161. err = fmt.Errorf("failed to parse your signing certificate to determine your team ID: %v", err)
  162. return "", err
  163. }
  164. if len(cert.Subject.OrganizationalUnit) == 0 {
  165. err = fmt.Errorf("the signing certificate has no organizational unit (team ID)")
  166. return "", err
  167. }
  168. return cert.Subject.OrganizationalUnit[0], nil
  169. }
  170. func appleCopyAssets(pkg *packages.Package, xcodeProjDir string) error {
  171. dstAssets := xcodeProjDir + "/main/assets"
  172. if err := mkdir(dstAssets); err != nil {
  173. return err
  174. }
  175. // TODO(hajimehoshi): This works only with Go tools that assume all source files are in one directory.
  176. // Fix this to work with other Go tools.
  177. srcAssets := filepath.Join(filepath.Dir(pkg.GoFiles[0]), "assets")
  178. fi, err := os.Stat(srcAssets)
  179. if err != nil {
  180. if os.IsNotExist(err) {
  181. // skip walking through the directory to deep copy.
  182. return nil
  183. }
  184. return err
  185. }
  186. if !fi.IsDir() {
  187. // skip walking through to deep copy.
  188. return nil
  189. }
  190. // if assets is a symlink, follow the symlink.
  191. srcAssets, err = filepath.EvalSymlinks(srcAssets)
  192. if err != nil {
  193. return err
  194. }
  195. return filepath.Walk(srcAssets, func(path string, info os.FileInfo, err error) error {
  196. if err != nil {
  197. return err
  198. }
  199. if name := filepath.Base(path); strings.HasPrefix(name, ".") {
  200. // Do not include the hidden files.
  201. return nil
  202. }
  203. if info.IsDir() {
  204. return nil
  205. }
  206. dst := dstAssets + "/" + path[len(srcAssets)+1:]
  207. return copyFile(dst, path)
  208. })
  209. }
  210. type infoplistTmplData struct {
  211. BundleID string
  212. Name string
  213. }
  214. var infoplistTmpl = template.Must(template.New("infoplist").Parse(`<?xml version="1.0" encoding="UTF-8"?>
  215. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  216. <plist version="1.0">
  217. <dict>
  218. <key>CFBundleDevelopmentRegion</key>
  219. <string>en</string>
  220. <key>CFBundleExecutable</key>
  221. <string>main</string>
  222. <key>CFBundleIdentifier</key>
  223. <string>{{.BundleID}}</string>
  224. <key>CFBundleInfoDictionaryVersion</key>
  225. <string>6.0</string>
  226. <key>CFBundleName</key>
  227. <string>{{.Name}}</string>
  228. <key>CFBundlePackageType</key>
  229. <string>APPL</string>
  230. <key>CFBundleShortVersionString</key>
  231. <string>1.0</string>
  232. <key>CFBundleSignature</key>
  233. <string>????</string>
  234. <key>CFBundleVersion</key>
  235. <string>1</string>
  236. <key>LSRequiresIPhoneOS</key>
  237. <true/>
  238. <key>UILaunchStoryboardName</key>
  239. <string>LaunchScreen</string>
  240. <key>UIRequiredDeviceCapabilities</key>
  241. <array>
  242. <string>armv7</string>
  243. </array>
  244. <key>UISupportedInterfaceOrientations</key>
  245. <array>
  246. <string>UIInterfaceOrientationPortrait</string>
  247. <string>UIInterfaceOrientationLandscapeLeft</string>
  248. <string>UIInterfaceOrientationLandscapeRight</string>
  249. </array>
  250. <key>UISupportedInterfaceOrientations~ipad</key>
  251. <array>
  252. <string>UIInterfaceOrientationPortrait</string>
  253. <string>UIInterfaceOrientationPortraitUpsideDown</string>
  254. <string>UIInterfaceOrientationLandscapeLeft</string>
  255. <string>UIInterfaceOrientationLandscapeRight</string>
  256. </array>
  257. </dict>
  258. </plist>
  259. `))
  260. type projPbxprojTmplData struct {
  261. TeamID string
  262. }
  263. var projPbxprojTmpl = template.Must(template.New("projPbxproj").Parse(`// !$*UTF8*$!
  264. {
  265. archiveVersion = 1;
  266. classes = {
  267. };
  268. objectVersion = 46;
  269. objects = {
  270. /* Begin PBXBuildFile section */
  271. 254BB84F1B1FD08900C56DE9 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 254BB84E1B1FD08900C56DE9 /* Images.xcassets */; };
  272. 254BB8681B1FD16500C56DE9 /* main in Resources */ = {isa = PBXBuildFile; fileRef = 254BB8671B1FD16500C56DE9 /* main */; };
  273. 25FB30331B30FDEE0005924C /* assets in Resources */ = {isa = PBXBuildFile; fileRef = 25FB30321B30FDEE0005924C /* assets */; };
  274. /* End PBXBuildFile section */
  275. /* Begin PBXFileReference section */
  276. 254BB83E1B1FD08900C56DE9 /* main.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = main.app; sourceTree = BUILT_PRODUCTS_DIR; };
  277. 254BB8421B1FD08900C56DE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
  278. 254BB84E1B1FD08900C56DE9 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
  279. 254BB8671B1FD16500C56DE9 /* main */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = main; sourceTree = "<group>"; };
  280. 25FB30321B30FDEE0005924C /* assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = assets; path = main/assets; sourceTree = "<group>"; };
  281. /* End PBXFileReference section */
  282. /* Begin PBXGroup section */
  283. 254BB8351B1FD08900C56DE9 = {
  284. isa = PBXGroup;
  285. children = (
  286. 25FB30321B30FDEE0005924C /* assets */,
  287. 254BB8401B1FD08900C56DE9 /* main */,
  288. 254BB83F1B1FD08900C56DE9 /* Products */,
  289. );
  290. sourceTree = "<group>";
  291. usesTabs = 0;
  292. };
  293. 254BB83F1B1FD08900C56DE9 /* Products */ = {
  294. isa = PBXGroup;
  295. children = (
  296. 254BB83E1B1FD08900C56DE9 /* main.app */,
  297. );
  298. name = Products;
  299. sourceTree = "<group>";
  300. };
  301. 254BB8401B1FD08900C56DE9 /* main */ = {
  302. isa = PBXGroup;
  303. children = (
  304. 254BB8671B1FD16500C56DE9 /* main */,
  305. 254BB84E1B1FD08900C56DE9 /* Images.xcassets */,
  306. 254BB8411B1FD08900C56DE9 /* Supporting Files */,
  307. );
  308. path = main;
  309. sourceTree = "<group>";
  310. };
  311. 254BB8411B1FD08900C56DE9 /* Supporting Files */ = {
  312. isa = PBXGroup;
  313. children = (
  314. 254BB8421B1FD08900C56DE9 /* Info.plist */,
  315. );
  316. name = "Supporting Files";
  317. sourceTree = "<group>";
  318. };
  319. /* End PBXGroup section */
  320. /* Begin PBXNativeTarget section */
  321. 254BB83D1B1FD08900C56DE9 /* main */ = {
  322. isa = PBXNativeTarget;
  323. buildConfigurationList = 254BB8611B1FD08900C56DE9 /* Build configuration list for PBXNativeTarget "main" */;
  324. buildPhases = (
  325. 254BB83C1B1FD08900C56DE9 /* Resources */,
  326. );
  327. buildRules = (
  328. );
  329. dependencies = (
  330. );
  331. name = main;
  332. productName = main;
  333. productReference = 254BB83E1B1FD08900C56DE9 /* main.app */;
  334. productType = "com.apple.product-type.application";
  335. };
  336. /* End PBXNativeTarget section */
  337. /* Begin PBXProject section */
  338. 254BB8361B1FD08900C56DE9 /* Project object */ = {
  339. isa = PBXProject;
  340. attributes = {
  341. LastUpgradeCheck = 0630;
  342. ORGANIZATIONNAME = Developer;
  343. TargetAttributes = {
  344. 254BB83D1B1FD08900C56DE9 = {
  345. CreatedOnToolsVersion = 6.3.1;
  346. DevelopmentTeam = {{.TeamID}};
  347. };
  348. };
  349. };
  350. buildConfigurationList = 254BB8391B1FD08900C56DE9 /* Build configuration list for PBXProject "main" */;
  351. compatibilityVersion = "Xcode 3.2";
  352. developmentRegion = English;
  353. hasScannedForEncodings = 0;
  354. knownRegions = (
  355. en,
  356. Base,
  357. );
  358. mainGroup = 254BB8351B1FD08900C56DE9;
  359. productRefGroup = 254BB83F1B1FD08900C56DE9 /* Products */;
  360. projectDirPath = "";
  361. projectRoot = "";
  362. targets = (
  363. 254BB83D1B1FD08900C56DE9 /* main */,
  364. );
  365. };
  366. /* End PBXProject section */
  367. /* Begin PBXResourcesBuildPhase section */
  368. 254BB83C1B1FD08900C56DE9 /* Resources */ = {
  369. isa = PBXResourcesBuildPhase;
  370. buildActionMask = 2147483647;
  371. files = (
  372. 25FB30331B30FDEE0005924C /* assets in Resources */,
  373. 254BB8681B1FD16500C56DE9 /* main in Resources */,
  374. 254BB84F1B1FD08900C56DE9 /* Images.xcassets in Resources */,
  375. );
  376. runOnlyForDeploymentPostprocessing = 0;
  377. };
  378. /* End PBXResourcesBuildPhase section */
  379. /* Begin XCBuildConfiguration section */
  380. 254BB8601B1FD08900C56DE9 /* Release */ = {
  381. isa = XCBuildConfiguration;
  382. buildSettings = {
  383. ALWAYS_SEARCH_USER_PATHS = NO;
  384. CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
  385. CLANG_CXX_LIBRARY = "libc++";
  386. CLANG_ENABLE_MODULES = YES;
  387. CLANG_ENABLE_OBJC_ARC = YES;
  388. CLANG_WARN_BOOL_CONVERSION = YES;
  389. CLANG_WARN_CONSTANT_CONVERSION = YES;
  390. CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
  391. CLANG_WARN_EMPTY_BODY = YES;
  392. CLANG_WARN_ENUM_CONVERSION = YES;
  393. CLANG_WARN_INT_CONVERSION = YES;
  394. CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
  395. CLANG_WARN_UNREACHABLE_CODE = YES;
  396. CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
  397. "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Apple Development";
  398. COPY_PHASE_STRIP = NO;
  399. DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
  400. ENABLE_NS_ASSERTIONS = NO;
  401. ENABLE_STRICT_OBJC_MSGSEND = YES;
  402. GCC_C_LANGUAGE_STANDARD = gnu99;
  403. GCC_NO_COMMON_BLOCKS = YES;
  404. GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
  405. GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
  406. GCC_WARN_UNDECLARED_SELECTOR = YES;
  407. GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
  408. GCC_WARN_UNUSED_FUNCTION = YES;
  409. GCC_WARN_UNUSED_VARIABLE = YES;
  410. MTL_ENABLE_DEBUG_INFO = NO;
  411. SDKROOT = iphoneos;
  412. TARGETED_DEVICE_FAMILY = "1,2";
  413. VALIDATE_PRODUCT = YES;
  414. ENABLE_BITCODE = YES;
  415. };
  416. name = Release;
  417. };
  418. 254BB8631B1FD08900C56DE9 /* Release */ = {
  419. isa = XCBuildConfiguration;
  420. buildSettings = {
  421. ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
  422. INFOPLIST_FILE = main/Info.plist;
  423. LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
  424. PRODUCT_NAME = "$(TARGET_NAME)";
  425. };
  426. name = Release;
  427. };
  428. /* End XCBuildConfiguration section */
  429. /* Begin XCConfigurationList section */
  430. 254BB8391B1FD08900C56DE9 /* Build configuration list for PBXProject "main" */ = {
  431. isa = XCConfigurationList;
  432. buildConfigurations = (
  433. 254BB8601B1FD08900C56DE9 /* Release */,
  434. );
  435. defaultConfigurationIsVisible = 0;
  436. defaultConfigurationName = Release;
  437. };
  438. 254BB8611B1FD08900C56DE9 /* Build configuration list for PBXNativeTarget "main" */ = {
  439. isa = XCConfigurationList;
  440. buildConfigurations = (
  441. 254BB8631B1FD08900C56DE9 /* Release */,
  442. );
  443. defaultConfigurationIsVisible = 0;
  444. defaultConfigurationName = Release;
  445. };
  446. /* End XCConfigurationList section */
  447. };
  448. rootObject = 254BB8361B1FD08900C56DE9 /* Project object */;
  449. }
  450. `))
  451. const contentsJSON = `{
  452. "images" : [
  453. {
  454. "idiom" : "iphone",
  455. "size" : "29x29",
  456. "scale" : "2x"
  457. },
  458. {
  459. "idiom" : "iphone",
  460. "size" : "29x29",
  461. "scale" : "3x"
  462. },
  463. {
  464. "idiom" : "iphone",
  465. "size" : "40x40",
  466. "scale" : "2x"
  467. },
  468. {
  469. "idiom" : "iphone",
  470. "size" : "40x40",
  471. "scale" : "3x"
  472. },
  473. {
  474. "idiom" : "iphone",
  475. "size" : "60x60",
  476. "scale" : "2x"
  477. },
  478. {
  479. "idiom" : "iphone",
  480. "size" : "60x60",
  481. "scale" : "3x"
  482. },
  483. {
  484. "idiom" : "ipad",
  485. "size" : "29x29",
  486. "scale" : "1x"
  487. },
  488. {
  489. "idiom" : "ipad",
  490. "size" : "29x29",
  491. "scale" : "2x"
  492. },
  493. {
  494. "idiom" : "ipad",
  495. "size" : "40x40",
  496. "scale" : "1x"
  497. },
  498. {
  499. "idiom" : "ipad",
  500. "size" : "40x40",
  501. "scale" : "2x"
  502. },
  503. {
  504. "idiom" : "ipad",
  505. "size" : "76x76",
  506. "scale" : "1x"
  507. },
  508. {
  509. "idiom" : "ipad",
  510. "size" : "76x76",
  511. "scale" : "2x"
  512. }
  513. ],
  514. "info" : {
  515. "version" : 1,
  516. "author" : "xcode"
  517. }
  518. }
  519. `
  520. // rfc1034Label sanitizes the name to be usable in a uniform type identifier.
  521. // The sanitization is similar to xcode's rfc1034identifier macro that
  522. // replaces illegal characters (not conforming the rfc1034 label rule) with '-'.
  523. func rfc1034Label(name string) string {
  524. // * Uniform type identifier:
  525. //
  526. // According to
  527. // https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html
  528. //
  529. // A uniform type identifier is a Unicode string that usually contains characters
  530. // in the ASCII character set. However, only a subset of the ASCII characters are
  531. // permitted. You may use the Roman alphabet in upper and lower case (A–Z, a–z),
  532. // the digits 0 through 9, the dot (“.”), and the hyphen (“-”). This restriction
  533. // is based on DNS name restrictions, set forth in RFC 1035.
  534. //
  535. // Uniform type identifiers may also contain any of the Unicode characters greater
  536. // than U+007F.
  537. //
  538. // Note: the actual implementation of xcode does not allow some unicode characters
  539. // greater than U+007f. In this implementation, we just replace everything non
  540. // alphanumeric with "-" like the rfc1034identifier macro.
  541. //
  542. // * RFC1034 Label
  543. //
  544. // <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
  545. // <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
  546. // <let-dig-hyp> ::= <let-dig> | "-"
  547. // <let-dig> ::= <letter> | <digit>
  548. const surrSelf = 0x10000
  549. begin := false
  550. var res []rune
  551. for i, r := range name {
  552. if r == '.' && !begin {
  553. continue
  554. }
  555. begin = true
  556. switch {
  557. case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z':
  558. res = append(res, r)
  559. case '0' <= r && r <= '9':
  560. if i == 0 {
  561. res = append(res, '-')
  562. } else {
  563. res = append(res, r)
  564. }
  565. default:
  566. if r < surrSelf {
  567. res = append(res, '-')
  568. } else {
  569. res = append(res, '-', '-')
  570. }
  571. }
  572. }
  573. return string(res)
  574. }