Commit the stream -- is a no-op if the existing code is identical to the generated code. Returns true if data was written and false if it wasn't (throws exception on io error)
| 1895 | // to the generated code. Returns true if data was written and false |
| 1896 | // if it wasn't (throws exception on io error) |
| 1897 | bool ExportsGenerator::commit(const std::string& preamble) { |
| 1898 | |
| 1899 | // get the generated code |
| 1900 | std::string code = codeStream_.str(); |
| 1901 | |
| 1902 | // if there is no generated code AND the exports file does not |
| 1903 | // currently exist then do nothing |
| 1904 | if (code.empty() && !FileInfo(targetFile_).exists()) |
| 1905 | return false; // #nocov |
| 1906 | |
| 1907 | // write header/preamble |
| 1908 | std::ostringstream headerStream; |
| 1909 | headerStream << commentPrefix_ << " Generated by using " |
| 1910 | << "Rcpp::compileAttributes()" |
| 1911 | << " -> do not edit by hand" << std::endl; |
| 1912 | headerStream << commentPrefix_ << " Generator token: " |
| 1913 | << generatorToken() << std::endl << std::endl; |
| 1914 | if (!preamble.empty()) |
| 1915 | headerStream << preamble; |
| 1916 | |
| 1917 | // get generated code and only write it if there was a change |
| 1918 | std::string generatedCode = headerStream.str() + code; |
| 1919 | if (generatedCode != existingCode_) { |
| 1920 | // open the file |
| 1921 | std::ofstream ofs(targetFile_.c_str(), |
| 1922 | std::ofstream::out | std::ofstream::trunc); |
| 1923 | if (ofs.fail()) |
| 1924 | throw Rcpp::file_io_error(targetFile_); // #nocov |
| 1925 | |
| 1926 | // write generated code and return |
| 1927 | ofs << generatedCode; |
| 1928 | ofs.close(); |
| 1929 | return true; |
| 1930 | } |
| 1931 | else { |
| 1932 | return false; // #nocov |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | // Remove the generated file entirely |
| 1937 | bool ExportsGenerator::remove() { |
no test coverage detected