|
| 1 | +/* |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +*/ |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +var fs = require('fs'); |
| 22 | +var CordovaError = require('cordova-common').CordovaError; |
| 23 | + |
| 24 | +function BridgingHeader (bridgingHeaderPath) { |
| 25 | + this.path = bridgingHeaderPath; |
| 26 | + this.bridgingHeaders = null; |
| 27 | + if (!fs.existsSync(this.path)) { |
| 28 | + throw new CordovaError('BridgingHeader.h is not found.'); |
| 29 | + } |
| 30 | + this.bridgingHeaders = this.__parseForBridgingHeader(fs.readFileSync(this.path, 'utf8')); |
| 31 | +} |
| 32 | + |
| 33 | +BridgingHeader.prototype.addHeader = function (plugin_id, header_path) { |
| 34 | + this.bridgingHeaders.push({type: 'code', code: '#import "' + header_path + '"\n'}); |
| 35 | +}; |
| 36 | + |
| 37 | +BridgingHeader.prototype.removeHeader = function (plugin_id, header_path) { |
| 38 | + this.bridgingHeaders = this.bridgingHeaders.filter(function (line) { |
| 39 | + if (this.found) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + if (line.type === 'code') { |
| 43 | + var re = new RegExp('#import\\s+"' + preg_quote(header_path) + '"(\\s*|\\s.+)(\\n|$)'); |
| 44 | + if (re.test(line.code)) { |
| 45 | + this.found = true; |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + return true; |
| 50 | + }, {found: false}); |
| 51 | +}; |
| 52 | + |
| 53 | +BridgingHeader.prototype.write = function () { |
| 54 | + var text = this.__stringifyForBridgingHeader(this.bridgingHeaders); |
| 55 | + fs.writeFileSync(this.path, text, 'utf8'); |
| 56 | +}; |
| 57 | + |
| 58 | +BridgingHeader.prototype.__stringifyForBridgingHeader = function (bridgingHeaders) { |
| 59 | + return bridgingHeaders.map(function (obj) { |
| 60 | + return obj.code; |
| 61 | + }).join(''); |
| 62 | +}; |
| 63 | + |
| 64 | +BridgingHeader.prototype.__parseForBridgingHeader = function (text) { |
| 65 | + var i = 0; |
| 66 | + var list = []; |
| 67 | + var type = 'code'; |
| 68 | + var start = 0; |
| 69 | + while (i < text.length) { |
| 70 | + switch (type) { |
| 71 | + case 'comment': |
| 72 | + if (i + 1 < text.length && text[i] === '*' && text[i + 1] === '/') { |
| 73 | + i += 2; |
| 74 | + list.push({type: type, code: text.slice(start, i)}); |
| 75 | + type = 'code'; |
| 76 | + start = i; |
| 77 | + } else { |
| 78 | + i += 1; |
| 79 | + } |
| 80 | + break; |
| 81 | + case 'line-comment': |
| 82 | + if (i < text.length && text[i] === '\n') { |
| 83 | + i += 1; |
| 84 | + list.push({type: type, code: text.slice(start, i)}); |
| 85 | + type = 'code'; |
| 86 | + start = i; |
| 87 | + } else { |
| 88 | + i += 1; |
| 89 | + } |
| 90 | + break; |
| 91 | + case 'code': |
| 92 | + default: |
| 93 | + if (i + 1 < text.length && text[i] === '/' && text[i + 1] === '*') { // comment |
| 94 | + if (start < i) { |
| 95 | + list.push({type: type, code: text.slice(start, i)}); |
| 96 | + } |
| 97 | + type = 'comment'; |
| 98 | + start = i; |
| 99 | + } else if (i + 1 < text.length && text[i] === '/' && text[i + 1] === '/') { // line comment |
| 100 | + if (start < i) { |
| 101 | + list.push({type: type, code: text.slice(start, i)}); |
| 102 | + } |
| 103 | + type = 'line-comment'; |
| 104 | + start = i; |
| 105 | + } else if (i < text.length && text[i] === '\n') { |
| 106 | + i += 1; |
| 107 | + list.push({type: type, code: text.slice(start, i)}); |
| 108 | + start = i; |
| 109 | + } else { |
| 110 | + i += 1; |
| 111 | + } |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + if (start < i) { |
| 116 | + list.push({type: type, code: text.slice(start, i)}); |
| 117 | + } |
| 118 | + return list; |
| 119 | +}; |
| 120 | + |
| 121 | +function preg_quote (str, delimiter) { |
| 122 | + return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); |
| 123 | +} |
| 124 | + |
| 125 | +module.exports.BridgingHeader = BridgingHeader; |
0 commit comments