main.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2017, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. var s = ""
  18. s += "/*\n"
  19. s += " *\n"
  20. s += " * Copyright 2017, Google Inc.\n"
  21. s += " * All rights reserved.\n"
  22. s += " *\n"
  23. s += " * Redistribution and use in source and binary forms, with or without\n"
  24. s += " * modification, are permitted provided that the following conditions are\n"
  25. s += " * met:\n"
  26. s += " *\n"
  27. s += " * * Redistributions of source code must retain the above copyright\n"
  28. s += " * notice, this list of conditions and the following disclaimer.\n"
  29. s += " * * Redistributions in binary form must reproduce the above\n"
  30. s += " * copyright notice, this list of conditions and the following disclaimer\n"
  31. s += " * in the documentation and/or other materials provided with the\n"
  32. s += " * distribution.\n"
  33. s += " * * Neither the name of Google Inc. nor the names of its\n"
  34. s += " * contributors may be used to endorse or promote products derived from\n"
  35. s += " * this software without specific prior written permission.\n"
  36. s += " *\n"
  37. s += " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  38. s += " * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  39. s += " * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  40. s += " * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
  41. s += " * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  42. s += " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  43. s += " * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  44. s += " * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  45. s += " * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  46. s += " * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  47. s += " * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  48. s += " *\n"
  49. s += " */\n"
  50. s += "// GENERATED: DO NOT EDIT\n"
  51. if CommandLine.arguments.contains("test") {
  52. s += "//\n"
  53. s += "// This file contain a function that returns the contents of Tests/ssl.crt and Tests/ssl.key.\n"
  54. s += "//\n"
  55. s += "import Foundation\n"
  56. s += "\n"
  57. s += "let certificateForTests = "
  58. let certificateURL = URL(fileURLWithPath: "Tests/ssl.crt")
  59. let certificateData = try Data(contentsOf: certificateURL)
  60. let encodedCertificate = certificateData.base64EncodedString()
  61. s += "Data(base64Encoded: \"\(encodedCertificate)\", options:[])!\n"
  62. s += "let keyForTests = "
  63. let keyURL = URL(fileURLWithPath: "Tests/ssl.key")
  64. let keyData = try Data(contentsOf: keyURL)
  65. let encodedKey = keyData.base64EncodedString()
  66. s += "Data(base64Encoded: \"\(encodedKey)\", options:[])!"
  67. } else {
  68. s += "//\n"
  69. s += "// This file contain a function that returns the default roots.pem.\n"
  70. s += "//\n"
  71. s += "import Foundation\n"
  72. s += "\n"
  73. s += "func roots_pem() -> String? {\n"
  74. let fileURL = URL(fileURLWithPath: "Assets/roots.pem")
  75. let filedata = try Data(contentsOf: fileURL)
  76. let encoding = filedata.base64EncodedString()
  77. s += " let roots = \n"
  78. s += " \"" + encoding + "\"\n"
  79. s += " if let data = Data(base64Encoded: roots, options:[]) {\n"
  80. s += " return String(data:data, encoding:.utf8)\n"
  81. s += " } else {\n"
  82. s += " return nil\n"
  83. s += " }\n"
  84. s += "}\n"
  85. }
  86. print(s)